import test from 'node:test'; import assert from 'node:assert/strict'; import { decideFile } from './freshness.js'; const full = { exists: true, rows: 2898, size: 350000, mtime: '2026-08-01T10:00:00.000Z' }; test('первый запуск принимает то, что есть', () => { assert.equal(decideFile(null, { exists: true, rows: 0, size: 0, mtime: null }).accept, true); assert.equal(decideFile(undefined, full).accept, true); }); test('файл исчез - держим прежние данные, а не обнуляем', () => { const decision = decideFile(full, { exists: false, rows: 0, size: 0, mtime: null }); assert.equal(decision.accept, false); assert.equal(decision.stale, true); assert.match(decision.warning, /недоступен/); }); test('файл, прочитанный в момент копирования, не принимается', () => { const decision = decideFile(full, { exists: true, rows: 4, size: 500, mtime: '2026-08-01T10:05:00.000Z' }); assert.equal(decision.accept, false); assert.equal(decision.stale, true); assert.match(decision.warning, /уменьшился/); assert.ok(decision.pending, 'запоминаем, как выглядел подозрительный файл'); }); test('то же самое чтение во второй раз принимается: файл стабилен', () => { const short = { exists: true, rows: 4, size: 500, mtime: '2026-08-01T10:05:00.000Z' }; const first = decideFile(full, short); const previousWithPending = { ...full, pending: first.pending }; const second = decideFile(previousWithPending, short); assert.equal(second.accept, true, 'дважды одинаковый файл считаем настоящим'); }); test('умеренное уменьшение принимается сразу', () => { const decision = decideFile(full, { exists: true, rows: 2400, size: 300000, mtime: '2026-08-01T11:00:00.000Z' }); assert.equal(decision.accept, true); }); test('маленькие файлы не блокируются: потеря меньше двадцати строк допустима', () => { const small = { exists: true, rows: 26, size: 6000, mtime: '2026-08-01T10:00:00.000Z' }; const decision = decideFile(small, { exists: true, rows: 12, size: 3000, mtime: '2026-08-01T11:00:00.000Z' }); assert.equal(decision.accept, true); }); test('рост файла принимается всегда', () => { const decision = decideFile(full, { exists: true, rows: 3200, size: 400000, mtime: '2026-08-01T11:00:00.000Z' }); assert.equal(decision.accept, true); }); test('пустой файл при прежних данных не принимается', () => { const decision = decideFile(full, { exists: true, rows: 0, size: 0, mtime: '2026-08-01T11:00:00.000Z' }); assert.equal(decision.accept, false); assert.equal(decision.stale, true); });