// Projects saved by earlier releases must keep loading and computing exactly // as they did. The fixtures were written by release a241adc; never edit them. import { readFileSync } from 'node:fs' import { beforeEach, describe, expect, it } from 'vitest' import { stats, truthTable } from '../app/lib/logic/analyze' import { compile } from '../app/lib/logic/compile' import { lookup } from '../app/lib/logic/ops' import { parseProject } from '../app/lib/logic/project' import { currentId, listProjects, loadProject, migrateLegacy } from '../app/lib/logic/storage' const fixture = readFileSync('tests/fixtures/v1-project.json', 'utf8') const expected = JSON.parse(readFileSync('tests/fixtures/v1-expected.json', 'utf8')) as Record const storage = JSON.parse(readFileSync('tests/fixtures/v1-localstorage.json', 'utf8')) as Record> function fakeLocalStorage(items: Record) { const map = new Map(Object.entries(items)) globalThis.localStorage = { getItem: k => map.get(k) ?? null, setItem: (k, v) => void map.set(k, String(v)), removeItem: k => void map.delete(k), clear: () => map.clear(), key: i => [...map.keys()][i] ?? null, get length() { return map.size }, } as Storage return map } describe('projects from release a241adc', () => { it('load with every component, wire, colour, corner and setting intact', () => { const before = JSON.parse(fixture) const p = parseProject(fixture) expect(p.name).toBe('Compat fixture') expect(p.settings.symbols).toBe('ansi') expect(p.solved).toEqual(['not']) expect(p.components.map(d => d.id)).toEqual(before.components.map((d: { id: string }) => d.id)) for (const d of before.components) { const now = p.components.find(c => c.id === d.id)! expect(now.nodes).toEqual(d.nodes) expect(now.wires).toEqual(d.wires) } }) it('compute the same truth tables, NAND counts and delays', () => { const p = parseProject(fixture) const get = lookup(p) for (const d of p.components) { const t = truthTable(get, d.id) expect({ stats: stats(compile(get, d.id)), table: 'error' in t ? t.error : t.rows.map(r => [...r.ins, '|', ...r.outs].join('')) }, d.name).toEqual(expected[d.id]) } }) }) describe('browser storage from earlier releases', () => { beforeEach(() => fakeLocalStorage({})) it('moves the first release’s single project into the library', () => { const map = fakeLocalStorage(storage.legacy!) migrateLegacy() const list = listProjects() expect(list).toHaveLength(1) expect(currentId()).toBe(list[0]!.id) expect(loadProject(list[0]!.id)!.components).toHaveLength(JSON.parse(fixture).components.length) expect(map.has('logigate:project:v1')).toBe(false) }) it('reads the current library, including share-link keys', () => { fakeLocalStorage(storage.library!) migrateLegacy() expect(listProjects()).toEqual([{ id: 'loc1', name: 'Compat fixture', updated: 1792235588166, link: { id: 'AbCdEfGh', key: 'secret-key' } }]) expect(currentId()).toBe('loc1') expect(loadProject('loc1')!.name).toBe('Compat fixture') }) })