test-schools.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env node
  2. 'use strict';
  3. // 学校数据(JSON 种子 + 运行时)单元测试
  4. // 用法:npm run test:schools
  5. const fs = require('fs');
  6. const os = require('os');
  7. const path = require('path');
  8. const results = [];
  9. function expect(cond, msg) {
  10. if (!cond) throw new Error(msg);
  11. }
  12. async function check(name, fn) {
  13. try {
  14. await fn();
  15. results.push({ name, ok: true });
  16. } catch (e) {
  17. results.push({ name, ok: false, error: e.message || String(e) });
  18. }
  19. }
  20. async function main() {
  21. const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'schools-test-'));
  22. const seedFile = path.join(tmp, 'seed.json');
  23. const runtimeFile = path.join(tmp, 'runtime.json');
  24. fs.writeFileSync(seedFile, JSON.stringify({
  25. version: 1,
  26. schools: [
  27. {
  28. id: 'sch_seed_1', name: '内置大学', aliases: ['内置'], province: '浙江省', city: '杭州市',
  29. lng: 120.1, lat: 30.2, enabled: true, seed: true,
  30. },
  31. {
  32. id: 'sch_seed_2', name: '坐标越界大学', province: '境外',
  33. lng: 10, lat: 60, enabled: true, seed: false,
  34. },
  35. ],
  36. }));
  37. process.env.SCHOOLS_SEED_FILE = seedFile;
  38. process.env.SCHOOLS_FILE = runtimeFile;
  39. const { loadSchools } = require(path.join(__dirname, '..', 'schools-store.js'));
  40. await check('读取种子:2 条', () => {
  41. const list = loadSchools();
  42. expect(list.length === 2, `实际 ${list.length}`);
  43. expect(list.every((s) => s.seed === true), '种子项应标记 seed');
  44. });
  45. await check('越界坐标降级为 null(未定位)', () => {
  46. const s = loadSchools().find((x) => x.name === '坐标越界大学');
  47. expect(s.lng === null && s.lat === null, `lng=${s.lng} lat=${s.lat}`);
  48. });
  49. await check('运行时不存在时只用种子', () => {
  50. expect(!fs.existsSync(runtimeFile), '运行时文件不应存在');
  51. expect(loadSchools().length === 2, '条数不对');
  52. });
  53. fs.writeFileSync(runtimeFile, JSON.stringify({
  54. version: 1,
  55. schools: [
  56. { name: '内置大学', enabled: false, province: '浙江省' },
  57. { id: 'sch_new_1', name: '新学校', province: '江苏省', city: '南京市', lng: 118.8, lat: 32.06, enabled: true },
  58. ],
  59. }));
  60. await check('运行时覆盖同名种子的字段', () => {
  61. const s = loadSchools().find((x) => x.name === '内置大学');
  62. expect(s.enabled === false, 'enabled 未被覆盖');
  63. expect(s.lng === 120.1, '未覆盖的字段应保留种子值');
  64. expect(s.seed === true, '种子标记不能丢');
  65. });
  66. await check('运行时新增学校并参与排序', () => {
  67. const list = loadSchools();
  68. expect(list.length === 3, `实际 ${list.length}`);
  69. const names = list.map((s) => s.name).join('|');
  70. expect(names.includes('新学校'), names);
  71. });
  72. await check('运行时项 seed=false', () => {
  73. expect(loadSchools().find((s) => s.name === '新学校').seed === false, 'seed 应为 false');
  74. });
  75. fs.writeFileSync(runtimeFile, '{ broken json');
  76. await check('运行时文件损坏:退回种子且不抛错', () => {
  77. const list = loadSchools();
  78. expect(list.length === 2, `实际 ${list.length}`);
  79. expect(list.some((s) => s.name === '内置大学'), '种子缺失');
  80. });
  81. try { fs.rmSync(tmp, { recursive: true, force: true }); } catch { /* ignore */ }
  82. const failed = results.filter((r) => !r.ok);
  83. console.log('\n========== 学校数据测试结果 ==========');
  84. for (const r of results) console.log(`${r.ok ? '✓' : '✗'} ${r.name}${r.ok ? '' : ' ← ' + r.error}`);
  85. console.log(`\n通过 ${results.length - failed.length}/${results.length}`);
  86. if (failed.length) process.exit(1);
  87. }
  88. main().catch((e) => {
  89. console.error('测试脚本自身异常:', e);
  90. process.exit(1);
  91. });