Repository navigation
Expand file tree
/
Copy pathopenspec-tracking.test.cjs
More file actions
494 lines (452 loc) · 15.6 KB
/
Copy pathopenspec-tracking.test.cjs
File metadata and controls
494 lines (452 loc) · 15.6 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// SPDX-License-Identifier: MIT
"use strict";
/**
* Tests for openspec-tracking.cjs.
*
* The planner is pure, so every case here is an inline input object. Only the
* directory scan touches disk, and it builds a throwaway tree in a temp
* directory rather than reading the committed `openspec/changes/`, which would
* make these tests pass or fail on whatever work happens to be in flight.
*
* Actions carry rendered prose, so most assertions read the fields that decide
* behaviour and leave the wording to the few cases that check it directly.
*/
const test = require("node:test");
const assert = require("node:assert/strict");
const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = require("node:fs");
const { tmpdir } = require("node:os");
const { join } = require("node:path");
const {
marker,
changeFromBody,
listUnarchivedChanges,
claimsFromPullRequests,
normalizeIssues,
issueBody,
planActions,
main,
runList,
} = require("./openspec-tracking.cjs");
/** Captures what `runList` writes to stdout, without a real process spawn. */
function captureList(changesDirectory) {
const original = process.stdout.write.bind(process.stdout);
let written = "";
process.stdout.write = (chunk) => {
written += chunk;
return true;
};
try {
runList(changesDirectory);
} finally {
process.stdout.write = original;
}
return JSON.parse(written);
}
/** The decision-bearing fields of an action, without the rendered prose. */
function shape(action) {
const { title, body, comment, ...rest } = action;
return rest;
}
function shapes(plan) {
return plan.actions.map(shape);
}
test("a marker round-trips through an issue body", () => {
assert.equal(changeFromBody(issueBody("my-change", "abc123")), "my-change");
assert.equal(changeFromBody(`prose\n\n${marker("other")}\n`), "other");
});
test("a body with no marker adopts nothing", () => {
assert.equal(changeFromBody("an unrelated issue"), undefined);
assert.equal(changeFromBody(undefined), undefined);
});
test("the issue body states the consequence and the command", () => {
const body = issueBody("some-change", "deadbee");
assert.match(body, /standing specs are wrong right now/);
assert.match(body, /pnpm openspec archive some-change/);
assert.match(body, /MODIFIED Requirements/);
assert.match(body, /deadbee/);
});
test("the scan lists change directories and skips archive", () => {
const root = mkdtempSync(join(tmpdir(), "openspec-tracking-"));
try {
const changes = join(root, "changes");
mkdirSync(join(changes, "archive", "2026-01-01-old"), { recursive: true });
mkdirSync(join(changes, "beta"), { recursive: true });
mkdirSync(join(changes, "alpha"), { recursive: true });
writeFileSync(join(changes, "README.md"), "not a directory");
assert.deepEqual(listUnarchivedChanges(changes), ["alpha", "beta"]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test("a missing changes directory is not a fault", () => {
assert.deepEqual(listUnarchivedChanges("/nonexistent/openspec/changes"), []);
});
test("--list prints the scan as a sorted JSON array, archive excluded", () => {
const root = mkdtempSync(join(tmpdir(), "openspec-tracking-list-"));
try {
const changes = join(root, "changes");
mkdirSync(join(changes, "archive", "2026-01-01-old"), { recursive: true });
mkdirSync(join(changes, "beta"), { recursive: true });
mkdirSync(join(changes, "alpha"), { recursive: true });
assert.deepEqual(captureList(changes), ["alpha", "beta"]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test("--list against a missing directory prints [], not a fault", () => {
assert.deepEqual(captureList("/nonexistent/openspec/changes"), []);
});
test("--list round-trips a change name containing a space", () => {
const root = mkdtempSync(join(tmpdir(), "openspec-tracking-list-space-"));
try {
const changes = join(root, "changes");
mkdirSync(join(changes, "probe with spaces"), { recursive: true });
mkdirSync(join(changes, "probe-normal"), { recursive: true });
assert.deepEqual(captureList(changes), [
"probe with spaces",
"probe-normal",
]);
} finally {
rmSync(root, { recursive: true, force: true });
}
});
test("a pull request touching a change directory claims it", () => {
const claims = claimsFromPullRequests([
{ number: 265, files: ["openspec/changes/thing/tasks.md", "src/a.ts"] },
{ number: 266, files: ["openspec/changes/thing/proposal.md"] },
{ number: 267, files: ["src/b.ts"] },
]);
assert.deepEqual(claims, { thing: [265, 266] });
});
test("touching the archive claims nothing", () => {
const claims = claimsFromPullRequests([
{ number: 1, files: ["openspec/changes/archive/2026-01-01-old/tasks.md"] },
]);
assert.deepEqual(claims, {});
});
test("a pull request with no files claims nothing", () => {
assert.deepEqual(claimsFromPullRequests([{ number: 1 }]), {});
});
test("the planner reduces pull requests to claims itself", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "in-flight" }],
pulls: [{ number: 266, files: ["openspec/changes/in-flight/tasks.md"] }],
issues: [],
});
assert.deepEqual(shapes(plan), []);
});
test("an unclaimed change with no issue opens one", () => {
const plan = planActions({
mode: "push",
sha: "abc123",
unarchived: [{ name: "orphan" }],
claims: {},
issues: [],
});
assert.deepEqual(shapes(plan), [{ type: "open", change: "orphan" }]);
assert.equal(plan.actions[0].title, "OpenSpec: orphan is unarchived on main");
assert.match(plan.actions[0].body, /<!-- openspec-tracking:orphan -->/);
});
test("a draining stack reports nothing", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "in-flight" }],
claims: { "in-flight": [266, 267] },
issues: [],
});
assert.deepEqual(shapes(plan), []);
});
test("a closed issue reopens rather than opening a second", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "orphan" }],
claims: {},
issues: [{ number: 12, state: "closed", change: "orphan" }],
});
assert.deepEqual(shapes(plan), [
{ type: "reopen", change: "orphan", issue: 12 },
]);
});
test("an open issue for an unclaimed change is left alone", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "orphan" }],
claims: {},
issues: [{ number: 12, state: "open", change: "orphan" }],
});
assert.deepEqual(shapes(plan), []);
});
test("archiving a change closes its issue", () => {
const plan = planActions({
mode: "push",
sha: "feedbee",
unarchived: [],
claims: {},
issues: [{ number: 12, state: "open", change: "landed" }],
});
assert.deepEqual(shapes(plan), [
{ type: "close", reason: "archived", change: "landed", issue: 12 },
]);
assert.match(plan.actions[0].comment, /Archived\./);
assert.match(plan.actions[0].comment, /feedbee/);
});
test("work resuming on a reported change closes its issue as claimed", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "resumed" }],
claims: { resumed: [300] },
issues: [{ number: 12, state: "open", change: "resumed" }],
});
assert.deepEqual(shapes(plan), [
{
type: "close",
reason: "claimed",
change: "resumed",
issue: 12,
claimants: [300],
},
]);
assert.match(plan.actions[0].comment, /Claimed again/);
assert.match(plan.actions[0].comment, /300/);
});
test("a closed issue for an archived change stays closed", () => {
const plan = planActions({
mode: "push",
unarchived: [],
claims: {},
issues: [{ number: 12, state: "closed", change: "landed" }],
});
assert.deepEqual(shapes(plan), []);
});
test("the sweep escalates only past the window", () => {
const input = {
mode: "sweep",
staleDays: 7,
claims: {},
issues: [{ number: 12, state: "open", change: "stalled" }],
};
assert.deepEqual(
shapes(planActions({ ...input, unarchived: [{ name: "stalled", ageDays: 6 }] })),
[]
);
assert.deepEqual(
shapes(planActions({ ...input, unarchived: [{ name: "stalled", ageDays: 7 }] })),
[{ type: "escalate", change: "stalled", issue: 12, ageDays: 7 }]
);
});
test("the sweep opens an issue when the push check never ran", () => {
const plan = planActions({
mode: "sweep",
staleDays: 7,
unarchived: [{ name: "missed", ageDays: 30 }],
claims: {},
issues: [],
});
assert.deepEqual(shapes(plan), [{ type: "open", change: "missed" }]);
});
test("the sweep ignores claims, because age is read from activity", () => {
const plan = planActions({
mode: "sweep",
staleDays: 7,
unarchived: [{ name: "stalled", ageDays: 30 }],
claims: { stalled: [400] },
issues: [],
});
assert.deepEqual(shapes(plan), [{ type: "open", change: "stalled" }]);
});
test("the sweep also closes an issue for an archived change", () => {
// The sweep backstops the push check never having been evaluated, and that
// applies to the success path too: a stale open issue against work that was
// archived is noise.
const plan = planActions({
mode: "sweep",
staleDays: 7,
unarchived: [],
claims: {},
issues: [{ number: 12, state: "open", change: "landed" }],
});
assert.deepEqual(shapes(plan), [
{ type: "close", reason: "archived", change: "landed", issue: 12 },
]);
});
test("the sweep does not parse task completion", () => {
// Two changes identical but for their tasks; the planner never sees tasks at
// all, so both cross the window on age alone.
const plan = planActions({
mode: "sweep",
staleDays: 7,
unarchived: [
{ name: "finished", ageDays: 10 },
{ name: "half-done", ageDays: 10 },
],
claims: {},
issues: [],
});
assert.deepEqual(shapes(plan), [
{ type: "open", change: "finished" },
{ type: "open", change: "half-done" },
]);
});
test("issues are matched from their body, so one regex exists rather than three", () => {
const plan = planActions({
mode: "push",
unarchived: [{ name: "orphan" }],
claims: {},
issues: [
{ number: 12, state: "CLOSED", body: `prose\n${marker("orphan")}\n` },
{ number: 13, state: "open", body: "an unrelated issue carrying the label" },
],
});
assert.deepEqual(shapes(plan), [
{ type: "reopen", change: "orphan", issue: 12 },
]);
});
test("an issue with no marker is never adopted and never closed", () => {
const plan = planActions({
mode: "push",
unarchived: [],
claims: {},
issues: [{ number: 13, state: "open", body: "someone else's issue" }],
});
assert.deepEqual(shapes(plan), []);
});
test("normalizeIssues lowercases state and parses the marker", () => {
assert.deepEqual(
normalizeIssues([
{ number: 1, state: "OPEN", body: marker("a"), idleDays: 3 },
{ number: 2, state: "open", body: "no marker" },
{ number: 3, state: "closed", change: "c" },
]),
[
{ number: 1, state: "open", change: "a", idleDays: 3 },
{ number: 3, state: "closed", change: "c", idleDays: undefined },
]
);
});
test("the sweep escalates at most once per window", () => {
const stalled = { name: "stalled", ageDays: 30 };
const input = { mode: "sweep", staleDays: 7, unarchived: [stalled], claims: {} };
// Escalated yesterday: `ageDays` keeps growing, but the issue was just
// commented on, so a second comment today would be the daily-nag failure.
assert.deepEqual(
shapes(
planActions({
...input,
issues: [{ number: 12, state: "open", change: "stalled", idleDays: 1 }],
})
),
[]
);
// Quiet for a full window: escalate again.
assert.deepEqual(
shapes(
planActions({
...input,
issues: [{ number: 12, state: "open", change: "stalled", idleDays: 7 }],
})
),
[{ type: "escalate", change: "stalled", issue: 12, ageDays: 30 }]
);
});
test("the sweep reopens a closed issue rather than commenting into it", () => {
// The defect this pins: `findIssue` runs over a `--state all` listing, so a
// closed issue satisfies `issue !== undefined` and used to fall straight
// through to `escalate`. Every other sweep case here passes an OPEN issue,
// which is why nothing caught it. Someone who closes a tracking issue
// without archiving the change then gets a daily comment appended to a
// closed thread, green and seen by nobody.
const plan = planActions({
mode: "sweep",
sha: "abc123",
staleDays: 7,
unarchived: [{ name: "stalled", ageDays: 30 }],
claims: {},
issues: [{ number: 9, state: "closed", body: marker("stalled"), idleDays: 30 }],
});
assert.deepEqual(shapes(plan), [
{ type: "reopen", change: "stalled", issue: 9, ageDays: 30 },
]);
// The comment states what the sweep measured. Push mode reopens on the claim
// test, which the sweep never consults, so it must not reuse that wording.
assert.match(plan.actions[0].comment, /no git\nactivity for 30 days/);
assert.doesNotMatch(plan.actions[0].comment, /pull request/);
});
test("the sweep reopens at most once per window", () => {
// The throttle governs a reopen too. A reopen is louder than a comment, so
// reopening on every daily run is the same nag, amplified.
const input = {
mode: "sweep",
staleDays: 7,
unarchived: [{ name: "stalled", ageDays: 30 }],
claims: {},
};
assert.deepEqual(
shapes(
planActions({
...input,
issues: [{ number: 9, state: "closed", change: "stalled", idleDays: 1 }],
})
),
[]
);
assert.deepEqual(
shapes(
planActions({
...input,
issues: [{ number: 9, state: "closed", change: "stalled", idleDays: 7 }],
})
),
[{ type: "reopen", change: "stalled", issue: 9, ageDays: 30 }]
);
});
test("the sweep leaves a closed issue alone inside the window", () => {
// Age gates before state does: a change closed and under the window is not
// the sweep's business, whatever the issue's state.
const plan = planActions({
mode: "sweep",
staleDays: 7,
unarchived: [{ name: "fresh", ageDays: 2 }],
claims: {},
issues: [{ number: 9, state: "closed", change: "fresh", idleDays: 30 }],
});
assert.deepEqual(shapes(plan), []);
});
test("an empty listing closes every tracked issue, so a caller must never guess it", () => {
// Pinning the hazard, not the feature. Absence from `unarchived` is read as
// "archived", so `[]` claims every change is archived. A workflow that could
// not read the directory and passed `[]` anyway would close every open
// tracking issue with a false comment. openspec-sweep.yml did exactly that
// until its apply step was gated on the listing having succeeded.
const plan = planActions({
mode: "sweep",
sha: "abc123",
staleDays: 7,
unarchived: [],
issues: [
{ number: 11, state: "open", body: marker("real-change") },
{ number: 12, state: "open", body: marker("other-change") },
],
});
assert.deepEqual(shapes(plan), [
{ type: "close", reason: "archived", change: "real-change", issue: 11 },
{ type: "close", reason: "archived", change: "other-change", issue: 12 },
]);
assert.match(plan.actions[0].comment, /reached `openspec\/changes\/archive\/`/);
});
test("an unknown mode is a caller defect, not a silent pass", () => {
assert.throws(() => planActions({ mode: "daily" }), /unknown mode/);
});
test("main parses an input document", () => {
const plan = main(
JSON.stringify({
mode: "push",
unarchived: [{ name: "orphan" }],
pulls: [],
issues: [],
})
);
assert.equal(plan.mode, "push");
assert.equal(plan.staleDays, 7);
assert.deepEqual(shapes(plan), [{ type: "open", change: "orphan" }]);
});