import { readFile, realpath, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { check } from '../../check/check.ts'; import type { KbGit } from '../../git/git.ts'; import { TAXONOMY_FILE } from '../../layout/index.ts'; import { makeFakeGit } from '../../test-utils/make-fake-git.ts'; import { makeStore } from '../../test-utils/make-store.ts'; import { makeTempDir } from '../../test-utils/make-temp-dir.ts'; import { getRegistryPathFor, seedRegistry } from '../../test-utils/registry.ts'; import { run } from '../run.ts'; const { check: realCheck } = await vi.importActual('../../check/check.ts'); // Mock `check` with a passthrough to the real implementation so that most tests run against real stores; a test that // needs a different result overrides it for one call. vi.mock('../../check/check.ts', async () => { const actual = await vi.importActual('../../check/check.ts'); return { ...actual, check: vi.fn(actual.check) }; }); const VALID = '---\ntitle: A\nrecordType: assertion\ncreated: 2026-05-01\nupdated: 2026-05-01\ntags: [x]\n---\n\nBody.\n'; const UNRESOLVED_LINK = '---\ntitle: Bad\nrecordType: assertion\ncreated: 2026-05-01\nupdated: 2026-05-01\ntags: [x]\n---\n\nSee [[Nonexistent]].\n'; describe(run, () => { afterEach(() => { vi.mocked(check).mockClear(); }); it('exits 0 with a clean-run line when the check does not report any findings', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toBe('PASS no findings (1 notes checked)\n'); }); it('prints the clean-run glyph in rich style when --output-style forces it, before or after the command', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const before = await run({ argv: ['--output-style', 'rich', 'check'], cwd: store }); const after = await run({ argv: ['check', '--output-style=rich'], cwd: store }); expect(before.stdout).toBe('✅ no findings (1 notes checked)\n'); expect(after.stdout).toBe('✅ no findings (1 notes checked)\n'); }); it('prints rich to a terminal outside CI, and plain to one in CI', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const terminal = await run({ argv: ['check'], cwd: store, env: {}, isTty: true }); const ci = await run({ argv: ['check'], cwd: store, env: { CI: 'true' }, isTty: true }); expect(terminal.stdout).toBe('✅ no findings (1 notes checked)\n'); expect(ci.stdout).toBe('PASS no findings (1 notes checked)\n'); }); it('takes the style from KB_OUTPUT_STYLE, which --output-style outranks', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const env = { KB_OUTPUT_STYLE: 'rich' }; const fromEnv = await run({ argv: ['check'], cwd: store, env }); const fromFlag = await run({ argv: ['check', '--output-style', 'plain'], cwd: store, env }); expect(fromEnv.stdout).toBe('✅ no findings (1 notes checked)\n'); expect(fromFlag.stdout).toBe('PASS no findings (1 notes checked)\n'); }); it('exits 2 for an --output-style value that does not name any style', async () => { const result = await run({ argv: ['check', '--output-style', 'fancy'], cwd: process.cwd() }); expect(result.exitCode).toBe(2); expect(result.stderr).toBe('kb: --output-style must be one of: auto, plain, rich (got "fancy")\n'); }); it('documents --output-style and KB_OUTPUT_STYLE in top-level help', async () => { const result = await run({ argv: ['--help'], cwd: process.cwd() }); expect(result.stdout).toContain('--output-style '); expect(result.stdout).toContain('KB_OUTPUT_STYLE'); }); it('exits 1 when an error-severity finding is present', async () => { const store = await makeStore({ 'content/Bad.md': UNRESOLVED_LINK }); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(1); expect(result.stdout).toContain('wikilinks.unresolved'); }); it('exits 0 without the clean-run check when the targets do not match any notes', async () => { const store = await makeStore({ 'Loose.md': VALID }); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toBe('content/**/*.md did not match any notes (0 checked)\n'); expect(result.stdout).not.toContain('PASS'); }); it('exits 2 for an unknown flag', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const result = await run({ argv: ['check', '--nope'], cwd: store }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('unknown flag: --nope'); }); it('exits 2 when discovery does not find a .kb/ directory and --kb is not given', async () => { const empty = await makeTempDir('kb-cli-empty-'); const result = await run({ argv: ['check'], cwd: empty }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('no .kb/'); }); it('exits 2 when --kb names an unregistered store', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const home = await makeHome('coding', store); const result = await run({ argv: ['check', '--kb', 'ghost'], cwd: store, home }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('"ghost"'); }); it('exits 2 when config.yaml is malformed', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); await writeFile(join(store, '.kb', 'config.yaml'), 'targets: [unterminated\n', 'utf8'); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('config.yaml'); }); it('exits 2 when tag-aliases.yaml is malformed', async () => { const store = await makeStore({ 'content/Clean.md': VALID, '.kb/tag-aliases.yaml': 'aliases: [unterminated\n', }); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('tag-aliases.yaml'); }); it('exits 0 with the warning shown when only warning-severity findings are present', async () => { const aliased = VALID.replace('tags: [x]', 'tags: [vcs]'); const store = await makeStore({ 'content/Aliased.md': aliased, '.kb/tag-aliases.yaml': 'aliases:\n git: [vcs]\n', }); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('warning tag-alias'); }); it('resolves a store by --kb from the registry and emits the JSON shape', async () => { const store = await makeStore({ 'content/Bad.md': UNRESOLVED_LINK }); const home = await makeHome('coding', store); const result = await run({ argv: ['check', '--kb', 'coding', '--json'], cwd: store, home }); expect(result.exitCode).toBe(1); const payload: unknown = JSON.parse(result.stdout); expect(payload).toMatchObject({ store: { name: 'coding', path: store }, summary: { notes: 1, total: 1, errors: 1, warnings: 0 }, }); }); it('names a discovered store from its registry entry in the JSON report', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const home = await makeHome('coding', store); const result = await run({ argv: ['check', '--json'], cwd: store, home }); const payload: unknown = JSON.parse(result.stdout); expect(payload).toMatchObject({ store: { name: 'coding', path: store } }); }); it('reports a discovered store without a registry entry as unnamed', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const home = await makeTempDir('kb-cli-home-'); const result = await run({ argv: ['check', '--json'], cwd: store, home }); const payload: unknown = JSON.parse(result.stdout); expect(payload).toMatchObject({ store: { name: null, path: store } }); }); it('resolves a store from a project-local registry entry', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const project = await makeTempDir('kb-cli-project-'); await seedRegistry(getRegistryPathFor(project), `kbs:\n local:\n path: ${store}\n`); const home = await makeTempDir('kb-cli-home-'); const result = await run({ argv: ['check', '--kb', 'local'], cwd: project, home }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('PASS no findings'); }); it('groups findings by file with severity, rule, and line in human output', async () => { const store = await makeStore({ 'content/Bad.md': UNRESOLVED_LINK }); const result = await run({ argv: ['check'], cwd: store }); expect(result.stdout).toContain(join(store, 'content', 'Bad.md')); expect(result.stdout).toMatch(/error wikilinks\.unresolved \(line \d+\): /); }); it('does not write files during a check', async () => { const store = await makeStore({ 'content/Note.md': VALID }); const before = await readFile(join(store, 'content', 'Note.md'), 'utf8'); await run({ argv: ['check'], cwd: store }); expect(await readFile(join(store, 'content', 'Note.md'), 'utf8')).toBe(before); }); it('prints top-level help for a bare invocation', async () => { const result = await run({ argv: [], cwd: tmpdir() }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('Usage: kb '); }); it('prints check help for check --help', async () => { const result = await run({ argv: ['check', '--help'], cwd: tmpdir() }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('Usage: kb check'); }); it('exits 2 for an unknown command', async () => { const result = await run({ argv: ['frobnicate'], cwd: tmpdir() }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('unknown command'); }); it('propagates a non-loader error from check rather than swallowing it as a config error', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); vi.mocked(check).mockRejectedValueOnce(new Error('checker crashed')); await expect(run({ argv: ['check'], cwd: store })).rejects.toThrow(/checker crashed/); }); }); describe('kb check targeting', () => { it('validates only the notes matched by a path argument', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Bad.md': UNRESOLVED_LINK }); const whole = await run({ argv: ['check'], cwd: store }); const scoped = await run({ argv: ['check', 'content/Clean.md'], cwd: store }); expect(whole.exitCode).toBe(1); expect(scoped.exitCode).toBe(0); expect(scoped.stdout).toContain('1 notes checked'); }); it('validates only the notes beneath a directory argument', async () => { const store = await makeStore({ 'content/sub/Clean.md': VALID, 'content/Bad.md': UNRESOLVED_LINK }); const result = await run({ argv: ['check', 'content/sub'], cwd: store }); expect(result.exitCode).toBe(0); }); it('exits 2 naming a path that does not match any note', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const result = await run({ argv: ['check', 'content/Ghost.md'], cwd: store }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('content/Ghost.md'); }); it('exits 2 when path arguments and --vs are combined', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const result = await run({ argv: ['check', 'content/Clean.md', '--vs=HEAD'], cwd: store }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('--vs'); }); it('exits 2 when --vs is given without a ref', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const result = await run({ argv: ['check', '--vs'], cwd: store }); expect(result.exitCode).toBe(2); }); it('prints a targeted empty-selection line distinct from the whole-vault line', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/assets/logo.png': 'x' }); const result = await run({ argv: ['check', 'content/assets'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('the given paths did not match any notes'); expect(result.stdout).not.toContain('content/**/*.md'); }); it('reflects the selected count in the JSON summary for a path argument', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Bad.md': UNRESOLVED_LINK }); const result = await run({ argv: ['check', 'content/Bad.md', '--json'], cwd: store }); const payload: unknown = JSON.parse(result.stdout); expect(result.exitCode).toBe(1); expect(payload).toMatchObject({ summary: { notes: 1, errors: 1 } }); }); it('validates only the notes changed since a ref', async () => { const store = await makeStore({ 'content/BadUnchanged.md': UNRESOLVED_LINK, 'content/GoodChanged.md': VALID }); const git = await makeChangedFilesGit(store, ['content/GoodChanged.md']); const whole = await run({ argv: ['check'], cwd: store, git }); const scoped = await run({ argv: ['check', '--vs=main'], cwd: store, git }); expect(whole.exitCode).toBe(1); expect(scoped.exitCode).toBe(0); }); it('exits 2 for an unresolvable --vs ref', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); const git = makeFakeGit({ findMergeBase: { ok: false, message: 'fatal: Not a valid object name no-such-ref' } }); const result = await run({ argv: ['check', '--vs=no-such-ref'], cwd: store, git }); expect(result.exitCode).toBe(2); expect(result.stderr).toContain('no-such-ref'); }); it('composes --kb with a path argument, narrowing within the named store', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Bad.md': UNRESOLVED_LINK }); const home = await makeHome('coding', store); const whole = await run({ argv: ['check', '--kb', 'coding'], cwd: store, home }); const scoped = await run({ argv: ['check', '--kb', 'coding', 'content/Clean.md'], cwd: store, home }); expect(whole.exitCode).toBe(1); expect(scoped.exitCode).toBe(0); }); it('checks only the changed metacharacter-named note, not its glob-expanded siblings', async () => { // `Draft2.md` is unchanged and erroring; as a glob, `Draft[v2].md` would also match it, so the // sibling must not fail a `--vs` run whose only changed note is the clean `Draft[v2].md`. const store = await makeStore({ 'content/Draft2.md': UNRESOLVED_LINK, 'content/Draft[v2].md': VALID }); const git = await makeChangedFilesGit(store, ['content/Draft[v2].md']); const result = await run({ argv: ['check', '--vs=main'], cwd: store, git }); expect(result.exitCode).toBe(0); }); it('checks a changed note with a non-ASCII name', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Café.md': UNRESOLVED_LINK }); const git = await makeChangedFilesGit(store, ['content/Café.md']); const result = await run({ argv: ['check', '--vs=main'], cwd: store, git }); expect(result.exitCode).toBe(1); }); }); describe('kb check vault-scoped findings', () => { it('reports a vault-scoped finding on a whole-vault run', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); stubVaultFinding(); const result = await run({ argv: ['check'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('taxonomy.undeclared'); }); it('keeps a vault-scoped finding under a path-targeted run', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Other.md': VALID }); stubVaultFinding(); const result = await run({ argv: ['check', 'content/Clean.md'], cwd: store }); expect(result.stdout).toContain('taxonomy.undeclared'); expect(result.stdout).toContain('in 1 notes'); }); it('keeps a vault-scoped finding under a --vs run', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Added.md': VALID }); const git = await makeChangedFilesGit(store, ['content/Added.md']); stubVaultFinding(); const result = await run({ argv: ['check', '--vs=main'], cwd: store, git }); expect(result.stdout).toContain('taxonomy.undeclared'); }); it('reports a vault-scoped finding after the zero-match line when the run did not check any notes', async () => { const store = await makeStore({ 'Loose.md': VALID }); stubVaultFinding(); const result = await run({ argv: ['check'], cwd: store }); expect(result.stdout).toContain('content/**/*.md did not match any notes (0 checked)'); expect(result.stdout).toContain('taxonomy.undeclared'); expect(result.stdout).toContain('in 0 notes'); }); it('drops an unselected note-scoped finding while keeping the vault-scoped one', async () => { const store = await makeStore({ 'content/Clean.md': VALID, 'content/Bad.md': UNRESOLVED_LINK }); stubVaultFinding(); const result = await run({ argv: ['check', 'content/Clean.md'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).not.toContain('wikilinks.unresolved'); expect(result.stdout).toContain('taxonomy.undeclared'); }); it('reports real taxonomy drift under a run targeting an unrelated note', async () => { const store = await makeStore({ '.kb/taxonomy.yaml': 'domains:\n languages: Programming languages\n', 'content/assertions/engineering/Note.md': VALID, 'content/assertions/languages/Other.md': VALID, }); const result = await run({ argv: ['check', 'content/assertions/languages/Other.md'], cwd: store }); expect(result.exitCode).toBe(0); expect(result.stdout).toContain('taxonomy.undeclared'); expect(result.stdout).toContain('"engineering"'); }); it('does not report any finding for a store that does not declare a taxonomy', async () => { const store = await makeStore({ 'content/assertions/engineering/Note.md': VALID }); const result = await run({ argv: ['check'], cwd: store }); expect(result.stdout).toBe('PASS no findings (1 notes checked)\n'); }); it('includes the scope in the JSON report', async () => { const store = await makeStore({ 'content/Clean.md': VALID }); stubVaultFinding(); const result = await run({ argv: ['check', '--json'], cwd: store }); const payload: unknown = JSON.parse(result.stdout); expect(payload).toMatchObject({ findings: [{ rule: 'taxonomy.undeclared', scope: 'vault' }] }); }); }); // region | Helpers /** Builds a git adapter for a `--vs` run in which git reports `files`, store-relative, as changed since the merge base. */ async function makeChangedFilesGit(store: string, files: readonly string[]): Promise { return makeFakeGit({ findMergeBase: { ok: true, value: 'base' }, findTopLevel: { ok: true, value: await realpath(store) }, listChangedFiles: { ok: true, value: [...files] }, }); } /** Creates an isolated home registering `name → storePath` in `~/.agents/kb.yaml`; returns the home dir. */ async function makeHome(name: string, storePath: string): Promise { const home = await makeTempDir('kb-cli-home-'); await seedRegistry(getRegistryPathFor(home), `kbs:\n ${name}:\n path: ${storePath}\n`); return home; } /** * Makes the next `check` call return its real result plus one vault-scoped finding, so that the CLI's handling of a * finding that describes the store rather than a note can be exercised independently of the rules that produce them. */ function stubVaultFinding(): void { vi.mocked(check).mockImplementationOnce(async (input) => { const result = await realCheck(input); return { ...result, findings: [ ...result.findings, { path: join(input.kbRoot, TAXONOMY_FILE), scope: 'vault', rule: 'taxonomy.undeclared', severity: 'warning', message: 'folder "engineering" contains notes but is not declared as a domain', }, ], }; }); } // endregion | Helpers