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

ThreadBackgroundLiveness.test.ts

173 lines5.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as ThreadBackgroundLiveness from "./ThreadBackgroundLiveness.ts";
3​
4describe("ThreadBackgroundLiveness", () => {
5 it("agents present as working; monitors as monitoring; agents win", () => {
6 const liveness = ThreadBackgroundLiveness.make();
7 const threadId = "t-live-1";
8 liveness.recordTaskLiveness({
9 threadId,
10 taskId: "m1",
11 taskType: "local_bash",
12 status: undefined,
13 kind: "started",
14 });
15 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
16 liveness.recordTaskLiveness({
17 threadId,
18 taskId: "a1",
19 taskType: "subagent",
20 status: undefined,
21 kind: "started",
22 });
23 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
24 liveness.recordTaskLiveness({
25 threadId,
26 taskId: "a1",
27 taskType: "subagent",
28 status: "completed",
29 kind: "completed",
30 });
31 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
32 liveness.recordTaskLiveness({
33 threadId,
34 taskId: "m1",
35 taskType: "local_bash",
36 status: "completed",
37 kind: "completed",
38 });
39 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
40 });
41​
42 it("terminal rows without a taskType still clear monitor entries", () => {
43 const liveness = ThreadBackgroundLiveness.make();
44 const threadId = "t-live-2";
45 liveness.recordTaskLiveness({
46 threadId,
47 taskId: "m1",
48 taskType: "local_bash",
49 status: undefined,
50 kind: "started",
51 });
52 // Terminal tick arrives with no taskType (common on task.completed).
53 liveness.recordTaskLiveness({
54 threadId,
55 taskId: "m1",
56 taskType: undefined,
57 status: "completed",
58 kind: "completed",
59 });
60 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
61 });
62​
63 it("nested agents (agentId + agent taskType) still count toward liveness", () => {
64 const liveness = ThreadBackgroundLiveness.make();
65 const threadId = "t-live-nested";
66 liveness.recordTaskLiveness({
67 threadId,
68 taskId: "n1",
69 taskType: "local_agent",
70 status: undefined,
71 kind: "started",
72 agentId: "owner",
73 });
74 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
75 liveness.recordTaskLiveness({
76 threadId,
77 taskId: "n1",
78 taskType: "local_agent",
79 status: "completed",
80 kind: "completed",
81 agentId: "owner",
82 });
83 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
84 });
85​
86 it("untyped rows count as agents; idle is not live; agent-owned tasks are ignored", () => {
87 const liveness = ThreadBackgroundLiveness.make();
88 const threadId = "t-live-3";
89 liveness.recordTaskLiveness({
90 threadId,
91 taskId: "wf:1",
92 taskType: undefined,
93 status: "running",
94 kind: "progress",
95 });
96 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
97 liveness.recordTaskLiveness({
98 threadId,
99 taskId: "wf:1",
100 taskType: undefined,
101 status: "idle",
102 kind: "updated",
103 });
104 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
105 liveness.recordTaskLiveness({
106 threadId,
107 taskId: "sh:1",
108 taskType: "local_bash",
109 status: undefined,
110 kind: "started",
111 agentId: "owner",
112 });
113 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
114 });
115​
116 it("reclassification moves a task between buckets instead of duplicating it", () => {
117 const liveness = ThreadBackgroundLiveness.make();
118 const threadId = "t-live-reclass";
119 // First seen without a taskType: counts as an agent.
120 liveness.recordTaskLiveness({
121 threadId,
122 taskId: "x1",
123 taskType: undefined,
124 status: "running",
125 kind: "started",
126 });
127 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
128 // Later transition reveals it's a shell: downgrade to monitoring, not
129 // a stale duplicate pinning "working".
130 liveness.recordTaskLiveness({
131 threadId,
132 taskId: "x1",
133 taskType: "local_bash",
134 status: "running",
135 kind: "progress",
136 });
137 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
138 // Turning out to be inert or agent-owned drops the prior entry too.
139 liveness.recordTaskLiveness({
140 threadId,
141 taskId: "x1",
142 taskType: "local_bash",
143 status: "running",
144 kind: "progress",
145 agentId: "owner",
146 });
147 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
148 });
149​
150 it("plan tasks are inert; clear removes everything; instances are isolated", () => {
151 const a = ThreadBackgroundLiveness.make();
152 const b = ThreadBackgroundLiveness.make();
153 a.recordTaskLiveness({
154 threadId: "t",
155 taskId: "p1",
156 taskType: "plan",
157 status: undefined,
158 kind: "started",
159 });
160 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
161 a.recordTaskLiveness({
162 threadId: "t",
163 taskId: "a1",
164 taskType: "local_workflow",
165 status: undefined,
166 kind: "started",
167 });
168 expect(a.getThreadBackgroundLiveness("t")).toBe("working");
169 expect(b.getThreadBackgroundLiveness("t")).toBeNull();
170 a.clearThreadLiveness("t");
171 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
172 });
173});