import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import * as XLSX from 'xlsx'; import { describe, expect, it } from 'vitest'; import { parseAbutments } from '../src/core/abut/parse'; import { parseProject, serializeProject, SCHEMA_VERSION, type ProjectFile } from '../src/core/project'; import { loadP1 } from './helpers'; const RAG = fileURLToPath(new URL('../../../RAG/운곡교P1입력.xlsx', import.meta.url)); const abutments = parseAbutments(XLSX.read(readFileSync(RAG), { type: 'buffer' })).abutments; function project(extra: Partial = {}): ProjectFile { return { schemaVersion: SCHEMA_VERSION, project: { name: '운곡교 교각', bridge: '운곡교', client: '', author: '', crs: 'KATEC', memo: '' }, source: { fileName: '운곡교P1입력.xlsx', sheets: ['교각', 'IP', '받침'] }, piers: [{ id: 'P1', input: loadP1() }], history: [], ...extra, }; } describe('교대 기록의 저장과 불러오기 (AB)', () => { it('the A1 record survives a save and open unchanged', () => { const text = serializeProject(project({ abutments })); const back = parseProject(text); expect(back.abutments).toEqual(abutments); }); it('an old file without abutments opens with none', () => { const back = parseProject(serializeProject(project())); expect(back.abutments).toBeUndefined(); }); it('a damaged abutment record is refused and the message names it', () => { const bad = JSON.parse(JSON.stringify(abutments)) as typeof abutments; bad[0].seatEl = NaN as unknown as number; expect(() => parseProject(serializeProject(project({ abutments: bad })))).toThrow('A1'); }); it('a wrong 형식 is refused', () => { const bad = JSON.parse(JSON.stringify(abutments)) as typeof abutments; (bad[0] as unknown as { type: string }).type = 'A3'; expect(() => parseProject(serializeProject(project({ abutments: bad })))).toThrow('A1 또는 A2'); }); it('the imported defaults are saved and opened with the edited values', () => { const edited = abutments.map((a) => ({ ...a, groundEl: 34.5 })); const back = parseProject(serializeProject(project({ abutments: edited, abutDefaults: abutments }))); expect(back.abutments![0].groundEl).toBe(34.5); expect(back.abutDefaults).toEqual(abutments); }); });