Files
harness-lab/experiments/backlog-index/tests/reader.test.mjs
T

154 lines
6.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { NORM_VERSION, normalizeIntent, normalizeTitle } from '../src/normalize.ts';
import { readCards } from '../src/reader.ts';
import { makeConfig, tempDir, writeCard, writeRawCard } from './helpers.mjs';
function card(overrides = {}) {
return {
id: 'T-1',
title: 'Контроль жизненного цикла задач',
status: 'To Do',
archived: false,
path: 'backlog/tasks/t-1.md',
type: null,
priority: null,
project: 'lab',
labels: [],
milestone: null,
parentId: null,
dependencies: [],
description: 'Задачи накапливаются, и дубликаты не видно.',
acceptanceCriteria: ['Поиск находит похожие карточки', 'Карточки не изменяются'],
fileHash: 'x',
...overrides,
};
}
test('intent ignores status, dates, metadata and checkbox state', () => {
const base = normalizeIntent(card());
const statusChanged = normalizeIntent(card({ status: 'Done' }));
const dated = normalizeIntent(card({ priority: 'high', labels: ['post-mvp'], milestone: 'm-9' }));
assert.equal(base.hash, statusChanged.hash);
assert.equal(base.hash, dated.hash);
assert.equal(base.normVersion, NORM_VERSION);
});
test('checking an acceptance criterion does not change the intent hash', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, {
id: 'T-10',
title: 'Индекс бэклога',
description: 'Описание',
acceptanceCriteria: [{ text: 'Первый критерий' }, { text: 'Второй критерий' }],
});
const first = readCards({ repoRoot: root, backlogDir: backlog });
const intentBefore = normalizeIntent(first.cards[0]);
writeCard(backlog, {
id: 'T-10',
title: 'Индекс бэклога',
description: 'Описание',
acceptanceCriteria: [{ text: 'Первый критерий', checked: true }, { text: 'Второй критерий' }],
});
const second = readCards({ repoRoot: root, backlogDir: backlog });
const intentAfter = normalizeIntent(second.cards[0]);
assert.equal(intentBefore.hash, intentAfter.hash);
assert.notEqual(first.cards[0].fileHash, second.cards[0].fileHash);
});
test('changing criterion text changes the intent hash', () => {
const intent = normalizeIntent(card({ acceptanceCriteria: ['Первый критерий'] }));
const changed = normalizeIntent(card({ acceptanceCriteria: ['Первый критерий изменён'] }));
assert.notEqual(intent.hash, changed.hash);
});
test('reader drops list markers and criterion index prefixes but keeps order', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, {
id: 'T-11',
title: 'Чтение карточек',
description: 'Первая строка\n\n Вторая строка ',
acceptanceCriteria: [{ text: 'А' }, { text: 'Б' }],
});
const result = readCards({ repoRoot: root, backlogDir: backlog });
assert.equal(result.errors.length, 0);
const read = result.cards[0];
assert.deepEqual(read.acceptanceCriteria, ['А', 'Б']);
const intent = normalizeIntent(read);
assert.ok(intent.text.includes('Первая строка\nВторая строка'));
assert.ok(!intent.text.includes('#1'));
assert.ok(!intent.text.includes('[ ]'));
});
test('reader excludes Definition of Done, comments and notes from the intent', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, {
id: 'T-12',
title: 'Только намерение',
description: 'Полезное описание',
acceptanceCriteria: [{ text: 'Критерий' }],
definitionOfDone: ['npm run check пройден'],
comments: 'Обсуждение реализации',
notes: 'Заметки исполнения',
});
const read = readCards({ repoRoot: root, backlogDir: backlog }).cards[0];
const intent = normalizeIntent(read);
assert.ok(!intent.text.includes('npm run check'));
assert.ok(!intent.text.includes('Обсуждение реализации'));
assert.ok(!intent.text.includes('Заметки исполнения'));
});
test('reader reads archived cards and marks them archived', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, { id: 'T-20', title: 'Активная', description: 'А' });
writeCard(backlog, { id: 'T-21', title: 'Архивная', description: 'Б', archived: true });
const result = readCards({ repoRoot: root, backlogDir: backlog });
assert.equal(result.cards.length, 2);
const archived = result.cards.find((item) => item.id === 'T-21');
assert.equal(archived.archived, true);
assert.ok(archived.path.startsWith('backlog/archive/tasks/'));
});
test('reader reports an unparseable card instead of failing silently', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, { id: 'T-30', title: 'Нормальная', description: 'А' });
writeRawCard(backlog, 'T-31', 'Просто текст без frontmatter\n');
const result = readCards({ repoRoot: root, backlogDir: backlog });
assert.equal(result.cards.length, 1);
assert.equal(result.errors.length, 1);
});
test('reader output is sorted by id and stable across runs', () => {
const root = tempDir();
const backlog = `${root}/backlog`;
writeCard(backlog, { id: 'T-40', title: 'Б', description: 'Б' });
writeCard(backlog, { id: 'T-39', title: 'А', description: 'А' });
const first = readCards({ repoRoot: root, backlogDir: backlog }).cards.map((item) => item.id);
const second = readCards({ repoRoot: root, backlogDir: backlog }).cards.map((item) => item.id);
assert.deepEqual(first, ['T-39', 'T-40']);
assert.deepEqual(first, second);
});
test('title normalization is language-aware and punctuation-insensitive', () => {
assert.equal(normalizeTitle(' Контроль -- жизненного цикла! '), 'контроль жизненного цикла');
assert.equal(
normalizeTitle('Контроль жизненного цикла'),
normalizeTitle('контроль — жизненного цикла'),
);
});
test('config loader resolves paths relative to the config file', async () => {
const root = tempDir();
const { configPath } = makeConfig(root);
const { loadConfig } = await import('../src/config.ts');
const config = loadConfig(configPath);
assert.equal(config.backlogDir, `${root}/backlog`);
assert.equal(config.provider.dimensions, 64);
});