// @vitest-environment node import { test, expect } from 'vitest'; import { MIN_GAP_MS, pack, shouldSnapshot, summarizeProject, unpack } from './versions.js'; test('a version is kept once per window of editing, and always the first', () => { const t = 1_800_000_000_000; expect(shouldSnapshot({ prevEditedAt: t, lastKeptAt: undefined })).toBe(true); expect(shouldSnapshot({ prevEditedAt: t, lastKeptAt: t - 60_000 })).toBe(false); // a minute after the last one expect(shouldSnapshot({ prevEditedAt: t, lastKeptAt: t - MIN_GAP_MS })).toBe(true); expect(shouldSnapshot({ prevEditedAt: t, lastKeptAt: t - 1000, force: true })).toBe(true); // before a restore expect(shouldSnapshot({ prevEditedAt: undefined, lastKeptAt: undefined })).toBe(false); // nothing to keep }); test('a stored copy round-trips and is smaller than the original', () => { const study = { id: 'a', title: 'T', chapters: [{ verses: Array.from({ length: 200 }, (_, i) => ({ number: i, text: 'In the beginning God created the heavens and the earth.' })) }] }; const packed = pack(study); expect(packed.length).toBeLessThan(JSON.stringify(study).length / 5); expect(unpack(packed)).toEqual(study); }); test('a study is summarised by its chunks and the words in its notes', () => { const s = summarizeProject({ chapters: [{ chunks: [{ observation: 'one two three', application: ' four ' }, { finalScript: 'a b' }] }, { chunks: [{}] }] }); expect(s).toEqual({ chunks: 3, words: 6 }); expect(summarizeProject(null)).toEqual({ chunks: 0, words: 0 }); });