Skip to content

Commit 0ef6ea7

Browse files
committed
chore: clean up WorkspaceMonitor and Inbox tests
- Remove unnecessary dispose() calls from tests that don't test dispose - Combine two "before initial setup" tests into one - Import ContextManager type properly instead of inline import() - Improve test names to describe behavior more precisely
1 parent 4e1cbd3 commit 0ef6ea7

File tree

2 files changed

+30
-61
lines changed

2 files changed

+30
-61
lines changed

test/unit/inbox.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,37 @@ describe("Inbox", () => {
5858
}
5959

6060
describe("message handling", () => {
61-
it("shows notification when a message arrives", async () => {
62-
const { inbox, stream } = await createInbox();
61+
it("shows notification with the message title", async () => {
62+
const { stream } = await createInbox();
6363

6464
stream.pushMessage(createNotification("Out of memory"));
6565

6666
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
6767
"Out of memory",
6868
);
69-
inbox.dispose();
7069
});
7170

72-
it("shows multiple notifications for successive messages", async () => {
73-
const { inbox, stream } = await createInbox();
71+
it("shows each notification independently (no dedup)", async () => {
72+
const { stream } = await createInbox();
7473

7574
stream.pushMessage(createNotification("First alert"));
7675
stream.pushMessage(createNotification("Second alert"));
7776

7877
expect(vscode.window.showInformationMessage).toHaveBeenCalledTimes(2);
79-
inbox.dispose();
8078
});
8179

8280
it("logs parse errors without showing notifications", async () => {
83-
const { inbox, stream } = await createInbox();
81+
const { stream } = await createInbox();
8482

8583
stream.pushError(new Error("bad json"));
8684

8785
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
88-
inbox.dispose();
8986
});
9087

9188
it("closes the socket on dispose", async () => {
9289
const stream = new MockEventStream<GetInboxNotificationResponse>();
9390
const { inbox } = await createInbox(stream);
91+
9492
inbox.dispose();
9593

9694
expect(stream.stream.close).toHaveBeenCalled();
@@ -100,17 +98,16 @@ describe("Inbox", () => {
10098
describe("disableNotifications", () => {
10199
it("suppresses notifications when enabled", async () => {
102100
config.set("coder.disableNotifications", true);
103-
const { inbox, stream } = await createInbox();
101+
const { stream } = await createInbox();
104102

105103
stream.pushMessage(createNotification("Out of memory"));
106104

107105
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
108-
inbox.dispose();
109106
});
110107

111108
it("shows notifications after re-enabling", async () => {
112109
config.set("coder.disableNotifications", true);
113-
const { inbox, stream } = await createInbox();
110+
const { stream } = await createInbox();
114111

115112
stream.pushMessage(createNotification("suppressed"));
116113
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
@@ -121,7 +118,6 @@ describe("Inbox", () => {
121118
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
122119
"visible",
123120
);
124-
inbox.dispose();
125121
});
126122
});
127123
});

test/unit/workspace/workspaceMonitor.test.ts

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
} from "coder/site/src/api/typesGenerated";
1919

2020
import type { CoderApi } from "@/api/coderApi";
21+
import type { ContextManager } from "@/core/contextManager";
2122

2223
function createMockClient(stream: MockEventStream<ServerSentEvent>) {
2324
return {
@@ -56,7 +57,7 @@ describe("WorkspaceMonitor", () => {
5657
ws,
5758
client,
5859
createMockLogger(),
59-
contextManager as unknown as import("@/core/contextManager").ContextManager,
60+
contextManager as unknown as ContextManager,
6061
);
6162
return { monitor, client, stream };
6263
}
@@ -67,25 +68,24 @@ describe("WorkspaceMonitor", () => {
6768
const changes: Workspace[] = [];
6869
monitor.onChange.event((ws) => changes.push(ws));
6970

70-
const updated = createWorkspace({ outdated: true });
71-
stream.pushMessage(workspaceEvent(updated));
71+
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
7272

7373
expect(changes).toHaveLength(1);
7474
expect(changes[0].outdated).toBe(true);
75-
monitor.dispose();
7675
});
7776

7877
it("logs parse errors without showing notifications", async () => {
79-
const { monitor, stream } = await createMonitor();
78+
const { stream } = await createMonitor();
79+
8080
stream.pushError(new Error("bad json"));
8181

8282
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
83-
monitor.dispose();
8483
});
8584

8685
it("closes the socket on dispose", async () => {
8786
const stream = new MockEventStream<ServerSentEvent>();
8887
const { monitor } = await createMonitor(createWorkspace(), stream);
88+
8989
monitor.dispose();
9090

9191
expect(stream.stream.close).toHaveBeenCalled();
@@ -94,35 +94,31 @@ describe("WorkspaceMonitor", () => {
9494

9595
describe("context and status bar", () => {
9696
it("sets coder.workspace.updatable context when workspace is outdated", async () => {
97-
const { monitor, stream } = await createMonitor();
97+
const { stream } = await createMonitor();
9898

9999
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
100100

101101
expect(contextManager.set).toHaveBeenCalledWith(
102102
"coder.workspace.updatable",
103103
true,
104104
);
105-
monitor.dispose();
106105
});
107106

108107
it("shows status bar when outdated, hides when not", async () => {
109-
const { monitor, stream } = await createMonitor();
108+
const { stream } = await createMonitor();
110109

111110
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
112111
expect(statusBar.show).toHaveBeenCalled();
113112

114113
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: false })));
115114
expect(statusBar.hide).toHaveBeenCalled();
116-
117-
monitor.dispose();
118115
});
119116
});
120117

121-
describe("notifications when enabled", () => {
118+
describe("notifications", () => {
122119
it("shows autostop notification when deadline is impending", async () => {
123120
const deadline = new Date(Date.now() + 1000 * 60 * 15).toISOString();
124-
const { monitor, stream } = await createMonitor();
125-
monitor.markInitialSetupComplete();
121+
const { stream } = await createMonitor();
126122

127123
stream.pushMessage(
128124
workspaceEvent(
@@ -135,7 +131,6 @@ describe("WorkspaceMonitor", () => {
135131
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
136132
expect.stringContaining("scheduled to shut down"),
137133
);
138-
monitor.dispose();
139134
});
140135

141136
it("shows deletion notification when deletion is impending", async () => {
@@ -152,7 +147,6 @@ describe("WorkspaceMonitor", () => {
152147
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
153148
expect.stringContaining("scheduled for deletion"),
154149
);
155-
monitor.dispose();
156150
});
157151

158152
it("shows not-running notification after initial setup", async () => {
@@ -170,37 +164,27 @@ describe("WorkspaceMonitor", () => {
170164
expect.anything(),
171165
expect.anything(),
172166
);
173-
monitor.dispose();
174167
});
175168

176-
it("does not show not-running notification before initial setup", async () => {
177-
const { monitor, stream } = await createMonitor();
178-
179-
stream.pushMessage(
180-
workspaceEvent(
181-
createWorkspace({ latest_build: { status: "stopped" } }),
182-
),
183-
);
184-
185-
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
186-
monitor.dispose();
187-
});
188-
189-
it("does not show deletion notification before initial setup", async () => {
169+
it("does not show deletion or not-running notifications before initial setup", async () => {
190170
const deletingAt = new Date(
191171
Date.now() + 1000 * 60 * 60 * 12,
192172
).toISOString();
193-
const { monitor, stream } = await createMonitor();
173+
const { stream } = await createMonitor();
194174

195175
stream.pushMessage(
196176
workspaceEvent(createWorkspace({ deleting_at: deletingAt })),
197177
);
178+
stream.pushMessage(
179+
workspaceEvent(
180+
createWorkspace({ latest_build: { status: "stopped" } }),
181+
),
182+
);
198183

199184
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
200-
monitor.dispose();
201185
});
202186

203-
it("shows outdated notification and fetches template details", async () => {
187+
it("fetches template details for outdated notification", async () => {
204188
const { monitor, stream, client } = await createMonitor();
205189
monitor.markInitialSetupComplete();
206190

@@ -214,13 +198,11 @@ describe("WorkspaceMonitor", () => {
214198
"Update",
215199
);
216200
});
217-
monitor.dispose();
218201
});
219202

220203
it("only notifies once per event type", async () => {
221204
const deadline = new Date(Date.now() + 1000 * 60 * 15).toISOString();
222-
const { monitor, stream } = await createMonitor();
223-
monitor.markInitialSetupComplete();
205+
const { stream } = await createMonitor();
224206

225207
const ws = createWorkspace({
226208
latest_build: { status: "running", deadline },
@@ -229,12 +211,11 @@ describe("WorkspaceMonitor", () => {
229211
stream.pushMessage(workspaceEvent(ws));
230212

231213
expect(vscode.window.showInformationMessage).toHaveBeenCalledTimes(1);
232-
monitor.dispose();
233214
});
234215
});
235216

236217
describe("disableUpdateNotifications", () => {
237-
it("suppresses outdated notification but allows other notifications", async () => {
218+
it("suppresses outdated notification but allows other types", async () => {
238219
config.set("coder.disableUpdateNotifications", true);
239220
const deadline = new Date(Date.now() + 1000 * 60 * 15).toISOString();
240221
const { monitor, stream, client } = await createMonitor();
@@ -253,7 +234,6 @@ describe("WorkspaceMonitor", () => {
253234
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
254235
expect.stringContaining("scheduled to shut down"),
255236
);
256-
monitor.dispose();
257237
});
258238

259239
it("shows outdated notification after re-enabling", async () => {
@@ -267,11 +247,9 @@ describe("WorkspaceMonitor", () => {
267247
config.set("coder.disableUpdateNotifications", false);
268248

269249
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
270-
271250
await vi.waitFor(() => {
272251
expect(client.getTemplate).toHaveBeenCalled();
273252
});
274-
monitor.dispose();
275253
});
276254
});
277255

@@ -306,11 +284,10 @@ describe("WorkspaceMonitor", () => {
306284
);
307285

308286
expect(vscode.window.showInformationMessage).not.toHaveBeenCalled();
309-
monitor.dispose();
310287
});
311288

312289
it("still updates context and status bar", async () => {
313-
const { monitor, stream } = await createMonitor();
290+
const { stream } = await createMonitor();
314291

315292
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
316293

@@ -319,7 +296,6 @@ describe("WorkspaceMonitor", () => {
319296
true,
320297
);
321298
expect(statusBar.show).toHaveBeenCalled();
322-
monitor.dispose();
323299
});
324300

325301
it("still fires onChange events", async () => {
@@ -330,13 +306,11 @@ describe("WorkspaceMonitor", () => {
330306
stream.pushMessage(workspaceEvent(createWorkspace({ outdated: true })));
331307

332308
expect(changes).toHaveLength(1);
333-
monitor.dispose();
334309
});
335310

336311
it("shows notifications after re-enabling", async () => {
337312
const deadline = new Date(Date.now() + 1000 * 60 * 15).toISOString();
338-
const { monitor, stream } = await createMonitor();
339-
monitor.markInitialSetupComplete();
313+
const { stream } = await createMonitor();
340314

341315
stream.pushMessage(
342316
workspaceEvent(
@@ -357,7 +331,6 @@ describe("WorkspaceMonitor", () => {
357331
),
358332
);
359333
expect(vscode.window.showInformationMessage).toHaveBeenCalledTimes(1);
360-
monitor.dispose();
361334
});
362335
});
363336
});

0 commit comments

Comments
 (0)