-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshutdown.test.ts
More file actions
206 lines (178 loc) · 6.09 KB
/
shutdown.test.ts
File metadata and controls
206 lines (178 loc) · 6.09 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { afterEach, describe, expect, test } from "bun:test";
import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
getNextTask,
loadRunState,
saveRunState,
} from "../src/core/run-state.js";
import type { RunState } from "../src/core/run-state.js";
const testDir = join(tmpdir(), `locus-shutdown-test-${Date.now()}`);
const INTERRUPT_ERROR = "Interrupted by user";
function markInProgressTasksInterrupted(state: RunState): void {
for (const task of state.tasks) {
if (task.status === "in_progress") {
task.status = "failed";
task.failedAt = new Date().toISOString();
task.error = INTERRUPT_ERROR;
}
}
}
afterEach(() => {
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true });
}
});
describe("shutdown state preservation", () => {
test("in_progress tasks are marked failed on interrupt simulation", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-001",
type: "sprint",
sprint: "v1",
branch: "locus/sprint-v1",
startedAt: new Date().toISOString(),
tasks: [
{
taskId: "1",
order: 1,
status: "done",
completedAt: new Date().toISOString(),
},
{ taskId: "2", order: 2, status: "in_progress" },
{ taskId: "3", order: 3, status: "pending" },
],
};
// Simulate what the shutdown handler does.
markInProgressTasksInterrupted(state);
saveRunState(testDir, state);
const loaded = loadRunState(testDir, "v1");
expect(loaded).not.toBeNull();
expect(loaded?.tasks[0].status).toBe("done");
expect(loaded?.tasks[1].status).toBe("failed"); // was in_progress
expect(loaded?.tasks[1].error).toBe(INTERRUPT_ERROR);
expect(loaded?.tasks[2].status).toBe("pending");
});
test("state file is written atomically (valid JSON after save)", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-002",
type: "parallel",
startedAt: new Date().toISOString(),
tasks: [
{ taskId: "10", order: 1, status: "in_progress" },
{ taskId: "11", order: 2, status: "pending" },
],
};
saveRunState(testDir, state);
// File should be valid JSON
const raw = readFileSync(
join(testDir, ".locus", "run-state", "_parallel.json"),
"utf-8",
);
const parsed = JSON.parse(raw);
expect(parsed.runId).toBe("run-test-002");
expect(parsed.tasks).toHaveLength(2);
});
test("multiple in_progress tasks are all marked failed", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-003",
type: "parallel",
startedAt: new Date().toISOString(),
tasks: [
{ taskId: "20", order: 1, status: "in_progress" },
{ taskId: "21", order: 2, status: "in_progress" },
{ taskId: "22", order: 3, status: "in_progress" },
],
};
// Simulate shutdown handler.
markInProgressTasksInterrupted(state);
saveRunState(testDir, state);
const loaded = loadRunState(testDir);
expect(loaded?.tasks.every((t) => t.status === "failed")).toBe(true);
expect(loaded?.tasks.every((t) => t.error === INTERRUPT_ERROR)).toBe(true);
});
test("done and failed tasks are preserved during shutdown", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-004",
type: "sprint",
sprint: "v2",
branch: "locus/sprint-v2",
startedAt: new Date().toISOString(),
tasks: [
{
taskId: "30",
order: 1,
status: "done",
completedAt: "2026-01-01T00:00:00Z",
pr: 100,
},
{
taskId: "31",
order: 2,
status: "failed",
failedAt: "2026-01-01T01:00:00Z",
error: "API limit",
},
{ taskId: "32", order: 3, status: "in_progress" },
{ taskId: "33", order: 4, status: "pending" },
],
};
// Simulate shutdown.
markInProgressTasksInterrupted(state);
saveRunState(testDir, state);
const loaded = loadRunState(testDir, "v2");
expect(loaded?.tasks[0].status).toBe("done");
expect(loaded?.tasks[0].pr).toBe(100);
expect(loaded?.tasks[1].status).toBe("failed");
expect(loaded?.tasks[1].error).toBe("API limit");
expect(loaded?.tasks[2].status).toBe("failed");
expect(loaded?.tasks[2].error).toBe(INTERRUPT_ERROR);
expect(loaded?.tasks[3].status).toBe("pending");
});
test("shutdown with empty task list", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-005",
type: "sprint",
sprint: "empty",
startedAt: new Date().toISOString(),
tasks: [],
};
saveRunState(testDir, state);
const loaded = loadRunState(testDir, "empty");
expect(loaded?.tasks).toHaveLength(0);
});
test("resumed state retries interrupted task first after shutdown save", () => {
mkdirSync(join(testDir, ".locus"), { recursive: true });
const state: RunState = {
runId: "run-test-006",
type: "sprint",
sprint: "resume-test",
branch: "locus/sprint-resume-test",
startedAt: new Date().toISOString(),
tasks: [
{
taskId: "40",
order: 1,
status: "done",
completedAt: "2026-01-01T00:00:00Z",
},
{ taskId: "41", order: 2, status: "in_progress" },
{ taskId: "42", order: 3, status: "pending" },
],
};
// Simulate shutdown.
markInProgressTasksInterrupted(state);
saveRunState(testDir, state);
// Simulate resume — failed interrupted task should be retried first.
const loaded = loadRunState(testDir, "resume-test");
const next = loaded ? getNextTask(loaded) : null;
expect(next?.taskId).toBe("41"); // The one that was in_progress
expect(next?.status).toBe("failed");
expect(next?.error).toBe(INTERRUPT_ERROR);
});
});