Files
fission-ai__openspec/test/cli-e2e/validate-archived-tasks.test.ts
Clay Good 83be9d113e feat(validate): add --archived to lint task completion of archived changes (#1604)
* feat(validate): add --archived to lint task completion of archived changes

`openspec validate --archived` scans every change under changes/archive/
and fails (exit 1) if any has unchecked tasks in tasks.md. This catches
changes archived with unfinished work — which the normal validate flow
never sees, since it only looks at active changes — and is meant for a
pre-commit or CI hook.

It is a standalone, opt-in scope: it returns before any existing bulk
path, so no current `validate` invocation changes behavior, and it does
not re-validate already-applied spec deltas. Reuses getTaskProgressForChange
(the same counter status/list/archive use) so task counting never forks,
and reads root.archiveDir so it is store-aware.

Closes #205

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(validate): fail loudly on archive read errors and unreadable task files

Address adversarial + CodeRabbit review of `validate --archived`:

- listArchivedChangeIds now returns [] only for ENOENT (missing archive
  dir) and rethrows permission/I/O/ENOTDIR errors, so a real archive-read
  failure exits 1 instead of silently reading as "no archived changes".
- Add getTaskProgressDetailForChange, which reports task files that exist
  but cannot be read; --archived turns those into an ERROR (naming the
  file) rather than silently counting them as zero tasks. The shared
  getTaskProgressForChange now wraps it and drops the detail, so
  status/list/archive totals are byte-identical.
- Start the spinner after listing so a thrown listing error never leaves
  a spinner running.

Adds regression tests (archive path is a file; archived tasks.md is
unreadable) and unit tests for the new detail variant. Docs: align the
--archived table verb and add a troubleshooting one-liner.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(validate): address CodeRabbit nits on archived-task tests

- Build the unreadable-fixture path from separate path.join components
  instead of a hard-coded Unix-separator string.
- Assert the reported unreadable path (canonicalized with
  realpathSync.native), not just the count, so a wrong path can't pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor(validate): address round-3 review of --archived

From three fresh adversarial reviews (scale/perf, flag/output-shape,
filesystem/security):

- perf: memoize schema→glob resolution across archived changes via a
  run-scoped SchemaGlobCache, so the same schema.yaml isn't re-parsed
  once per change (the archive is append-only and can hold thousands).
  Threaded as an optional arg; existing callers are unchanged. Loop stays
  sequential by design (per-change work is synchronous) — now documented.
- output shape: issue `path` now follows validate's convention —
  'tasks.md' for incomplete tasks, and the POSIX root-relative file path
  for an unreadable file (one issue per file) instead of the bare 'tasks'.
- plain output: print `change/<id>` (matching the JSON `type` and bulk
  validation) instead of `archived/<id>`.
- docs: correct the "Never throws" docstrings (glob resolution can throw
  on a malformed/unsafe schema; the caller guards it) and note the
  load-bearing projectRoot override for the archive path depth.

Store-mode resolution confirmed correct by review. Tests updated + a memo
regression test added.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-11 20:53:06 +00:00

161 lines
5.6 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { promises as fs } from 'fs';
import path from 'path';
import { tmpdir } from 'os';
import { runCLI } from '../helpers/run-cli.js';
describe('openspec validate --archived checks archived task completion (#205)', () => {
let projectDir: string;
const write = async (relative: string, content: string) => {
const file = path.join(projectDir, relative);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, content, 'utf-8');
};
beforeAll(async () => {
projectDir = await fs.mkdtemp(path.join(tmpdir(), 'openspec-archived-tasks-e2e-'));
// Fully completed archived change.
await write(
'openspec/changes/archive/2026-01-01-done-change/tasks.md',
['# Tasks', '', '- [x] 1.1 do a', '- [x] 1.2 do b', ''].join('\n')
);
// Archived change with an unchecked nested sub-task.
await write(
'openspec/changes/archive/2026-01-02-incomplete-change/tasks.md',
[
'# Tasks',
'',
'- [x] 1.1 do a',
'- [ ] 1.2 do b',
' - [ ] 1.2.1 nested unfinished work',
'',
].join('\n')
);
// An active change with unchecked tasks must NOT be scanned by --archived.
await write(
'openspec/changes/active-change/tasks.md',
['# Tasks', '', '- [ ] 1.1 still in progress', ''].join('\n')
);
});
afterAll(async () => {
await fs.rm(projectDir, { recursive: true, force: true });
});
it('fails when an archived change has unchecked tasks and passes the complete one', async () => {
const result = await runCLI(['validate', '--archived', '--json'], {
cwd: projectDir,
});
expect(result.exitCode).toBe(1);
const report = JSON.parse(result.stdout);
const byId = Object.fromEntries(
report.items.map((item: { id: string; valid: boolean }) => [item.id, item.valid])
);
// Only archived changes are considered; the active change is absent.
expect(byId['active-change']).toBeUndefined();
expect(byId['2026-01-01-done-change']).toBe(true);
expect(byId['2026-01-02-incomplete-change']).toBe(false);
const incomplete = report.items.find(
(item: { id: string }) => item.id === '2026-01-02-incomplete-change'
);
// The unchecked nested sub-task counts, so 2 of 3 tasks are open.
expect(incomplete.issues[0]).toEqual(
expect.objectContaining({
level: 'ERROR',
path: 'tasks.md',
message: expect.stringContaining('2 incomplete tasks (1/3 completed)'),
})
);
});
it('exits 0 when every archived change is complete', async () => {
await fs.rm(
path.join(
projectDir,
'openspec/changes/archive/2026-01-02-incomplete-change'
),
{ recursive: true, force: true }
);
const result = await runCLI(['validate', '--archived'], { cwd: projectDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('✓ change/2026-01-01-done-change');
});
it('exits 0 with a friendly message when there is no archive directory', async () => {
const emptyDir = await fs.mkdtemp(path.join(tmpdir(), 'openspec-no-archive-e2e-'));
await fs.mkdir(path.join(emptyDir, 'openspec', 'changes'), { recursive: true });
await fs.mkdir(path.join(emptyDir, 'openspec', 'specs'), { recursive: true });
const result = await runCLI(['validate', '--archived'], { cwd: emptyDir });
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('No archived changes found.');
await fs.rm(emptyDir, { recursive: true, force: true });
});
it('fails instead of passing silently when the archive path is not a directory', async () => {
const dir = await fs.mkdtemp(path.join(tmpdir(), 'openspec-archive-notdir-e2e-'));
await fs.mkdir(path.join(dir, 'openspec', 'changes'), { recursive: true });
await fs.mkdir(path.join(dir, 'openspec', 'specs'), { recursive: true });
// A real read failure (ENOTDIR) must not read as "no archived changes".
await fs.writeFile(
path.join(dir, 'openspec', 'changes', 'archive'),
'not a directory\n'
);
const result = await runCLI(['validate', '--archived'], { cwd: dir });
expect(result.exitCode).toBe(1);
expect(result.stdout).not.toContain('No archived changes found.');
await fs.rm(dir, { recursive: true, force: true });
});
it('fails when an archived tasks file exists but cannot be read', async () => {
const dir = await fs.mkdtemp(path.join(tmpdir(), 'openspec-archive-unreadable-e2e-'));
await fs.mkdir(path.join(dir, 'openspec', 'specs'), { recursive: true });
// A tasks.md that is a directory triggers a non-ENOENT read error (EISDIR)
// on every platform, standing in for a genuinely unreadable file. It must
// be reported, not silently counted as "no tasks".
await fs.mkdir(
path.join(
dir,
'openspec',
'changes',
'archive',
'unreadable-change',
'tasks.md'
),
{ recursive: true }
);
const result = await runCLI(['validate', '--archived', '--json'], {
cwd: dir,
});
expect(result.exitCode).toBe(1);
const report = JSON.parse(result.stdout);
const item = report.items.find(
(i: { id: string }) => i.id === 'unreadable-change'
);
expect(item.valid).toBe(false);
expect(item.issues[0]).toEqual(
expect.objectContaining({
level: 'ERROR',
// Pathed like every other validate issue: POSIX, root-relative.
path: 'openspec/changes/archive/unreadable-change/tasks.md',
message: 'could not read task file',
})
);
await fs.rm(dir, { recursive: true, force: true });
});
});