#!/usr/bin/env node 'use strict'; // 学校数据(JSON 种子 + 运行时)单元测试 // 用法:npm run test:schools const fs = require('fs'); const os = require('os'); const path = require('path'); const results = []; function expect(cond, msg) { if (!cond) throw new Error(msg); } async function check(name, fn) { try { await fn(); results.push({ name, ok: true }); } catch (e) { results.push({ name, ok: false, error: e.message || String(e) }); } } async function main() { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'schools-test-')); const seedFile = path.join(tmp, 'seed.json'); const runtimeFile = path.join(tmp, 'runtime.json'); fs.writeFileSync(seedFile, JSON.stringify({ version: 1, schools: [ { id: 'sch_seed_1', name: '内置大学', aliases: ['内置'], province: '浙江省', city: '杭州市', lng: 120.1, lat: 30.2, enabled: true, seed: true, }, { id: 'sch_seed_2', name: '坐标越界大学', province: '境外', lng: 10, lat: 60, enabled: true, seed: false, }, ], })); process.env.SCHOOLS_SEED_FILE = seedFile; process.env.SCHOOLS_FILE = runtimeFile; const { loadSchools } = require(path.join(__dirname, '..', 'schools-store.js')); await check('读取种子:2 条', () => { const list = loadSchools(); expect(list.length === 2, `实际 ${list.length}`); expect(list.every((s) => s.seed === true), '种子项应标记 seed'); }); await check('越界坐标降级为 null(未定位)', () => { const s = loadSchools().find((x) => x.name === '坐标越界大学'); expect(s.lng === null && s.lat === null, `lng=${s.lng} lat=${s.lat}`); }); await check('运行时不存在时只用种子', () => { expect(!fs.existsSync(runtimeFile), '运行时文件不应存在'); expect(loadSchools().length === 2, '条数不对'); }); fs.writeFileSync(runtimeFile, JSON.stringify({ version: 1, schools: [ { name: '内置大学', enabled: false, province: '浙江省' }, { id: 'sch_new_1', name: '新学校', province: '江苏省', city: '南京市', lng: 118.8, lat: 32.06, enabled: true }, ], })); await check('运行时覆盖同名种子的字段', () => { const s = loadSchools().find((x) => x.name === '内置大学'); expect(s.enabled === false, 'enabled 未被覆盖'); expect(s.lng === 120.1, '未覆盖的字段应保留种子值'); expect(s.seed === true, '种子标记不能丢'); }); await check('运行时新增学校并参与排序', () => { const list = loadSchools(); expect(list.length === 3, `实际 ${list.length}`); const names = list.map((s) => s.name).join('|'); expect(names.includes('新学校'), names); }); await check('运行时项 seed=false', () => { expect(loadSchools().find((s) => s.name === '新学校').seed === false, 'seed 应为 false'); }); fs.writeFileSync(runtimeFile, '{ broken json'); await check('运行时文件损坏:退回种子且不抛错', () => { const list = loadSchools(); expect(list.length === 2, `实际 ${list.length}`); expect(list.some((s) => s.name === '内置大学'), '种子缺失'); }); try { fs.rmSync(tmp, { recursive: true, force: true }); } catch { /* ignore */ } const failed = results.filter((r) => !r.ok); console.log('\n========== 学校数据测试结果 =========='); for (const r of results) console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`); console.log(`\n通过 ${results.length - failed.length}/${results.length}`); if (failed.length) process.exit(1); } main().catch((e) => { console.error('测试脚本自身异常:', e); process.exit(1); });