-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
157 lines (135 loc) · 4.28 KB
/
Copy pathtest.js
File metadata and controls
157 lines (135 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import assert from "node:assert/strict";
import { DumbZoneAlert } from "./dumb-zone-alert.js";
const calls = [];
let hasDbusSocket = false;
function makeShellResult(val) {
const p = Promise.resolve(val);
p.text = () => Promise.resolve(val);
p.env = () => p;
return p;
}
function mock$(strings, ...values) {
const cmd = strings.reduce((acc, s, i) => acc + s + (values[i] ?? ""), "");
calls.push(cmd);
if (cmd.startsWith("which ")) {
const binary = cmd.slice("which ".length).trim();
if (!binary || binary === "notify-send-throw") {
return Promise.reject(new Error(`not found: ${binary}`));
}
return makeShellResult("/usr/bin/" + binary);
}
if (cmd.startsWith("test -S ")) {
if (!hasDbusSocket) {
return Promise.reject(new Error("socket not found"));
}
return makeShellResult("");
}
return makeShellResult("");
}
function mockClient() {
const toasts = [];
return {
app: {
log: async () => {},
},
tui: {
showToast: async ({ body }) => {
toasts.push(body);
},
},
_toasts: toasts,
};
}
async function reset() {
calls.length = 0;
hasDbusSocket = false;
}
function makeEvent(sessionId, input, cacheRead) {
return {
type: "session.updated",
properties: {
sessionID: sessionId,
info: {
tokens: {
input,
cache: { read: cacheRead ?? 0 },
},
},
},
};
}
// --- Test 1: notify-send not found ---
{
await reset();
const client = mockClient();
const plugin = await DumbZoneAlert({
client,
$: mock$,
directory: "/test/project",
});
// hack: override $
await plugin.event({ event: makeEvent("s1", 0, 0) });
await plugin.event({ event: makeEvent("s1", 60_000, 0) });
await plugin.event({ event: makeEvent("s1", 100_000, 0) });
const notifyCalls = calls.filter((c) => c.startsWith("notify-send"));
assert.equal(notifyCalls.length, 0, "should not call notify-send when binary missing");
assert.ok(client._toasts.length > 0, "should still fire TUI toasts");
console.log("PASS: notify-send not found -> skipped, TUI toasts still fire");
}
// --- Test 2: notify-send found, no D-Bus ---
{
await reset();
calls.length = 0;
const client = mockClient();
const plugin = await DumbZoneAlert({
client,
$: mock$,
directory: "/test/project",
});
await plugin.event({ event: makeEvent("s2", 0, 0) });
await plugin.event({ event: makeEvent("s2", 60_000, 0) });
await plugin.event({ event: makeEvent("s2", 100_000, 0) });
const notifyCalls2 = calls.filter((c) => c.startsWith("notify-send"));
assert.equal(notifyCalls2.length, 0, "should not call notify-send without D-Bus socket");
assert.ok(client._toasts.length > 0, "should still fire TUI toasts");
console.log("PASS: notify-send found, no D-Bus socket -> skipped, TUI toasts still fire");
}
// --- Test 3: notify-send found + D-Bus present ---
{
await reset();
hasDbusSocket = true;
calls.length = 0;
const client = mockClient();
const plugin = await DumbZoneAlert({
client,
$: mock$,
directory: "/test/project",
});
await plugin.event({ event: makeEvent("s3", 0, 0) });
// Jump past all thresholds in one delta
await plugin.event({ event: makeEvent("s3", 120_000, 0) });
const notifyCalls3 = calls.filter((c) => c.startsWith("notify-send"));
assert.equal(notifyCalls3.length, 3, "should call notify-send for 50%, 75%, and 100% thresholds");
assert.ok(client._toasts.length > 0, "should also fire TUI toasts");
console.log("PASS: notify-send found + D-Bus present -> notify-send called for thresholds");
}
// --- Test 4: multiple sessions are independent ---
{
await reset();
hasDbusSocket = true;
calls.length = 0;
const client = mockClient();
const plugin = await DumbZoneAlert({
client,
$: mock$,
directory: "/test/project",
});
await plugin.event({ event: makeEvent("s4a", 0, 0) });
await plugin.event({ event: makeEvent("s4b", 0, 0) });
await plugin.event({ event: makeEvent("s4a", 100_000, 0) });
await plugin.event({ event: makeEvent("s4b", 50_000, 0) });
const notifyCalls4 = calls.filter((c) => c.startsWith("notify-send"));
assert.equal(notifyCalls4.length, 4, "each session tracks independently");
console.log("PASS: multiple sessions tracked independently");
}
console.log("\nAll tests passed!");