27 lines
4.4 KiB
JavaScript
27 lines
4.4 KiB
JavaScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import fs from 'node:fs';
|
||
import { spawnSync } from 'node:child_process';
|
||
import { normalizeTasks } from '../src/normalize.mjs';
|
||
import { summarize } from '../src/summary.mjs';
|
||
import { renderText } from '../src/render.mjs';
|
||
const input = () => JSON.parse(fs.readFileSync('fixtures/tasks.json', 'utf8'));
|
||
const record = overrides => ({ id: 'one', title: '任务', status: 'todo', minutes: 5, ...overrides });
|
||
const expected = { total: 4, counts: { todo: 0, doing: 1, done: 2, blocked: 1 }, completedPercent: 50, remainingMinutes: 50 };
|
||
const invoke = (...args) => spawnSync(process.execPath, ['src/cli.mjs', ...args], { encoding: 'utf8', windowsHide: true });
|
||
|
||
test('normalize trims identifiers and titles into independent records', () => { const original = input(), before = structuredClone(original), result = normalizeTasks(original); assert.equal(result[0].id, 'docs'); assert.equal(result[0].title, '编写交付文档'); assert.notEqual(result, original); assert.notEqual(result[1], original[1]); assert.deepEqual(original, before); });
|
||
test('normalize rejects blank and duplicate normalized identifiers', () => { assert.throws(() => normalizeTasks([record({ id: ' ' })])); assert.throws(() => normalizeTasks([record({ title: ' ' })])); assert.throws(() => normalizeTasks([record(), record({ id: ' one ' })])); });
|
||
test('normalize validates record shape and bounded collection size', () => { assert.throws(() => normalizeTasks({})); assert.throws(() => normalizeTasks([null])); assert.throws(() => normalizeTasks(Array.from({ length: 10001 }, (_, i) => record({ id: String(i) })))); });
|
||
test('normalize supports all four states and rejects unknown states', () => { for (const status of ['todo', 'doing', 'done', 'blocked']) assert.equal(normalizeTasks([record({ status })])[0].status, status); assert.throws(() => normalizeTasks([record({ status: 'completed' })])); });
|
||
test('normalize rejects noninteger and out-of-range estimates', () => { for (const minutes of ['5', null, -1, 0.5, NaN, Infinity, 1000001]) assert.throws(() => normalizeTasks([record({ minutes })])); assert.equal(normalizeTasks([record({ minutes: 0 })])[0].minutes, 0); assert.equal(normalizeTasks([record({ minutes: 1000000 })])[0].minutes, 1000000); });
|
||
test('normalize emits only the declared four-field contract', () => assert.deepEqual(normalizeTasks([record({ unrelated: true })])[0], record()));
|
||
test('summary reports complete state counts and excludes completed work from remaining time', () => assert.deepEqual(summarize(normalizeTasks(input())), expected));
|
||
test('summary rounds progress instead of truncating percentages', () => assert.equal(summarize([record({ status: 'done' }), record({ id: 'two', status: 'done' }), record({ id: 'three' })]).completedPercent, 67));
|
||
test('summary handles an empty plan with every state present', () => assert.deepEqual(summarize([]), { total: 0, counts: { todo: 0, doing: 0, done: 0, blocked: 0 }, completedPercent: 0, remainingMinutes: 0 }));
|
||
test('text rendering keeps the required three readable Chinese lines', () => assert.equal(renderText(expected), '交付进度:2/4(50%)\n待办 0 · 进行中 1 · 已完成 2 · 阻塞 1\n剩余预计:50 分钟'));
|
||
test('CLI JSON output matches the summary contract and has a trailing newline', () => { const result = invoke('fixtures/tasks.json', '--json'); assert.equal(result.status, 0); assert.equal(result.stderr, ''); assert.deepEqual(JSON.parse(result.stdout), expected); assert.ok(result.stdout.endsWith('\n')); });
|
||
test('CLI text output is readable and matches the shared rendering', () => { const result = invoke('fixtures/tasks.json'); assert.equal(result.status, 0); assert.equal(result.stdout, renderText(expected) + '\n'); assert.equal(result.stderr, ''); });
|
||
test('CLI missing arguments and unknown options return exit 2 without output', () => { for (const args of [[], ['fixtures/tasks.json', '--unknown'], ['fixtures/tasks.json', '--json', '--json']]) { const result = invoke(...args); assert.equal(result.status, 2); assert.equal(result.stdout, ''); assert.match(result.stderr, /^输入错误:/); } });
|
||
test('CLI invalid JSON and missing files preserve the failure exit code', () => { for (const file of ['fixtures/invalid.json', 'fixtures/not-found.json']) { const result = invoke(file); assert.equal(result.status, 2); assert.equal(result.stdout, ''); assert.match(result.stderr, /^输入错误:/); } });
|