36 lines
1.9 KiB
JavaScript
36 lines
1.9 KiB
JavaScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import { mkdtempSync, rmSync, readFileSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { upsertIndexEntry, writeIndexSafely } from './secretary-index.mjs';
|
|
|
|
describe('upsertIndexEntry', () => {
|
|
it('добавляет новое дело', () => {
|
|
const md = upsertIndexEntry('', { slug: 'sec', title: 'Секретарь', goal: 'память сути', status: 'открыто', date: '2026-06-21' });
|
|
expect(md).toContain('[Секретарь](sec/protocol.md)');
|
|
expect(md).toContain('открыто');
|
|
});
|
|
it('обновляет существующее дело без дубля', () => {
|
|
const first = upsertIndexEntry('', { slug: 'sec', title: 'Секретарь', goal: 'g', status: 'открыто', date: '2026-06-21' });
|
|
const upd = upsertIndexEntry(first, { slug: 'sec', title: 'Секретарь', goal: 'g', status: 'закрыто', date: '2026-06-22' });
|
|
expect(upd.match(/sec\/protocol\.md/g).length).toBe(1);
|
|
expect(upd).toContain('закрыто');
|
|
});
|
|
});
|
|
|
|
describe('writeIndexSafely', () => {
|
|
let dir, idx;
|
|
beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'secidx-')); idx = join(dir, 'содержание.md'); writeFileSync(idx, ''); });
|
|
afterEach(() => { rmSync(dir, { recursive: true, force: true }); });
|
|
|
|
it('two concurrent upserts of different themes both survive', async () => {
|
|
await Promise.all([
|
|
writeIndexSafely(idx, { slug: 'alpha', title: 'alpha', goal: 'g', status: 'открыто', date: 'd' }),
|
|
writeIndexSafely(idx, { slug: 'beta', title: 'beta', goal: 'g', status: 'открыто', date: 'd' }),
|
|
]);
|
|
const md = readFileSync(idx, 'utf-8');
|
|
expect(md).toContain('(alpha/protocol.md)');
|
|
expect(md).toContain('(beta/protocol.md)');
|
|
});
|
|
});
|