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

terminalStateCleanup.test.ts

65 lines2.3 KB

1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime";
2import { ThreadId } from "@t3tools/contracts";
3import { describe, expect, it } from "vitest";
4​
5import { collectActiveTerminalThreadIds } from "./terminalStateCleanup";
6​
7const threadId = (id: string): ThreadId => ThreadId.make(id);
8const threadKey = (environmentId: string, id: string): string =>
9 scopedThreadKey(scopeThreadRef(environmentId as never, threadId(id)));
10​
11describe("collectActiveTerminalThreadIds", () => {
12 it("retains non-deleted server threads", () => {
13 const activeThreadIds = collectActiveTerminalThreadIds({
14 snapshotThreads: [
15 { key: threadKey("env-a", "server-1"), deletedAt: null, archivedAt: null },
16 { key: threadKey("env-b", "server-2"), deletedAt: null, archivedAt: null },
17 ],
18 draftThreadKeys: [],
19 });
20​
21 expect(activeThreadIds).toEqual(
22 new Set([threadKey("env-a", "server-1"), threadKey("env-b", "server-2")]),
23 );
24 });
25​
26 it("ignores deleted and archived server threads and keeps local draft threads", () => {
27 const activeThreadIds = collectActiveTerminalThreadIds({
28 snapshotThreads: [
29 { key: threadKey("env-a", "server-active"), deletedAt: null, archivedAt: null },
30 {
31 key: threadKey("env-a", "server-deleted"),
32 deletedAt: "2026-03-05T08:00:00.000Z",
33 archivedAt: null,
34 },
35 {
36 key: threadKey("env-a", "server-archived"),
37 deletedAt: null,
38 archivedAt: "2026-03-05T09:00:00.000Z",
39 },
40 ],
41 draftThreadKeys: [threadKey("env-a", "local-draft")],
42 });
43​
44 expect(activeThreadIds).toEqual(
45 new Set([threadKey("env-a", "server-active"), threadKey("env-a", "local-draft")]),
46 );
47 });
48​
49 it("does not keep draft-linked terminal state for archived server threads", () => {
50 const archivedThreadId = threadKey("env-a", "server-archived");
51​
52 const activeThreadIds = collectActiveTerminalThreadIds({
53 snapshotThreads: [
54 {
55 key: archivedThreadId,
56 deletedAt: null,
57 archivedAt: "2026-03-05T09:00:00.000Z",
58 },
59 ],
60 draftThreadKeys: [archivedThreadId, threadKey("env-a", "local-draft")],
61 });
62​
63 expect(activeThreadIds).toEqual(new Set([threadKey("env-a", "local-draft")]));
64 });
65});