Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

closingTerminals.test.ts

34 lines1.3 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { expect, it } from "vite-plus/test";
3​
4import { closingTerminalIds, holdTerminalIdWhileClosing } from "./closingTerminals";
5​
6function deferred() {
7 let resolve!: () => void;
8 let reject!: (error: Error) => void;
9 const promise = new Promise<void>((onResolve, onReject) => {
10 resolve = onResolve;
11 reject = onReject;
12 });
13 return { promise, resolve, reject };
15​
16it("keeps a closing terminal's ID out of reach until its last close settles", async () => {
17 const thread = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
18 const otherThread = { ...thread, threadId: ThreadId.make("other") };
19 const first = deferred();
20 const second = deferred();
21​
22 const firstClose = holdTerminalIdWhileClosing(thread, "term-1", () => first.promise);
23 const secondClose = holdTerminalIdWhileClosing(thread, "term-1", () => second.promise);
24 expect(closingTerminalIds(thread)).toEqual(["term-1"]);
25 expect(closingTerminalIds(otherThread)).toEqual([]);
26​
27 first.reject(new Error("close failed"));
28 await expect(firstClose).rejects.toThrow("close failed");
29 expect(closingTerminalIds(thread)).toEqual(["term-1"]);
30​
31 second.resolve();
32 await secondClose;
33 expect(closingTerminalIds(thread)).toEqual([]);
34});