legacy-appointment-pull.test.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import vm from 'node:vm';
  4. import { readFileSync } from 'node:fs';
  5. const gatewaySource = readFileSync(new URL('../deploy-admin-functions.mjs', import.meta.url), 'utf8');
  6. const source = gatewaySource.match(/const LEGACY_APPOINTMENT_PULLS=new Map\(\),LEGACY_APPOINTMENT_VERSIONS=new Map\(\);[\s\S]*?\nconst APP_LIVE_SCOPES=/)?.[0]
  7. .replace(/\nconst APP_LIVE_SCOPES=[\s\S]*$/, '');
  8. assert.ok(source, '未找到旧系统预约拉取实现');
  9. test('老师排课和运营排课投影保留交付方式,并识别历史线下标签', () => {
  10. const pick = name => gatewaySource.match(new RegExp('function '+name+'\\(.*?\\) \\{[\\s\\S]*?\\n\\}'))?.[0];
  11. const mobile = vm.createContext({ number: Number, firstValue: (...values) => values.find(value => value != null && value !== '') });
  12. vm.runInContext(pick('appOverviewScheduleRow'), mobile);
  13. const booking = { generalId: 12, deliveryMode: 'offline', jffs: '线下' };
  14. assert.equal(mobile.appOverviewScheduleRow(booking).deliveryMode, 'offline');
  15. assert.equal(mobile.appOverviewScheduleRow({ ...booking, deliveryMode: undefined, jffs: '上门' }).jffs, '上门');
  16. const admin = vm.createContext({
  17. number: (value, fallback = 0) => Number(value ?? fallback),
  18. text: value => String(value ?? '').trim(),
  19. appointmentDate: () => new Date('2026-12-02T00:00:00Z'),
  20. durationMinutes: () => 60,
  21. legacyOf: () => ({}),
  22. classType: () => '60 分钟',
  23. statusLabel: () => '待开始',
  24. dateIso: () => '',
  25. });
  26. const deliveryFunctions = gatewaySource.match(/function deliveryMode\([^\n]+\nfunction deliveryLabel\([^\n]+/)?.[0];
  27. vm.runInContext(deliveryFunctions+'\n'+pick('appointmentProjection'), admin);
  28. assert.equal(admin.appointmentProjection({ ...booking, deliveryMode: 'online' }).deliveryMode, 'online');
  29. assert.equal(admin.appointmentProjection({ jffs: '线上' }).deliveryMode, 'online');
  30. for (const jffs of ['线下', '上门', '机构']) {
  31. assert.equal(admin.appointmentProjection({ jffs }).deliveryMode, 'offline');
  32. }
  33. assert.equal(admin.appointmentProjection({}).deliveryMode, undefined);
  34. });
  35. function contextFor(rows) {
  36. const context = {
  37. console,
  38. URLSearchParams,
  39. AbortSignal,
  40. Map,
  41. Date,
  42. String,
  43. Promise,
  44. number(value, fallback = 0) { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : fallback; },
  45. firstValue(...values) { return values.find((value) => value !== undefined && value !== null && value !== ''); },
  46. appOverviewScheduleRow(row) {
  47. return {
  48. objectId: row.objectId,
  49. GeneralID: Number(row.generalId),
  50. ID: Number(row.generalId),
  51. Title: row.title,
  52. Title2: row.courseTitle,
  53. cancelled: Number(row.commonStatus) === -2,
  54. dszt: row.dszt,
  55. szyh: row.szyh,
  56. pl: row.pl,
  57. bxrq: row.bxrq,
  58. yysj: row.yysj,
  59. sdsd: row.sdsd,
  60. dslx: row.dslx,
  61. };
  62. },
  63. async appLegacyConfig() { return { url: 'https://legacy.example/api', apiId: 'id', apiKey: 'key' }; },
  64. async fetch(url) {
  65. assert.match(String(url), /myfield2=szyh%3D42/);
  66. return { ok: true, async text() { return JSON.stringify({ retcode: 1, result: JSON.stringify(rows), page: { itemCount: rows.length, pageCount: 1 } }); } };
  67. },
  68. };
  69. vm.runInNewContext(`${source};this.normalize=legacyAppointmentRecord;this.sync=performLegacyAppointmentSync;this.merge=mergeCompanionRows;this.dateKey=companionDateKey`, context);
  70. return context;
  71. }
  72. test('旧系统预约按学员读取并保留日期、状态与课程信息', async () => {
  73. const rows = [
  74. { GeneralID: 859161, Title: '灵灵(60分钟练习)', Subtitle: '阅读写作', szyh: 42, pl: 155, kcid: 398, bxrq: '2026-09-26', yysj: '2026-09-26 19:00', sdsd: '19:00', dslx: 2, dszt: 0, Status: 99 },
  75. { GeneralID: 900001, Title: '其他学员', szyh: 7, pl: 155, bxrq: '2026-09-27', dszt: 0, Status: 99 },
  76. ];
  77. const context = contextFor(rows);
  78. assert.equal(context.normalize(rows[1], 'szyh', 42), null);
  79. const result = await context.sync('szyh', 42);
  80. assert.equal(result.state, 'current');
  81. assert.equal(result.items.length, 1);
  82. assert.equal(result.items[0].GeneralID, 859161);
  83. assert.equal(result.items[0].Title, '灵灵(60分钟练习)');
  84. assert.equal(result.items[0].dszt, 0);
  85. assert.equal(context.dateKey(result.items[0]), '2026-09-26');
  86. });
  87. test('旧系统预约覆盖过期快照并补入后续计划', () => {
  88. const context = contextFor([]);
  89. const stored = [{ GeneralID: 1, dszt: 0, bxrq: '2026-09-02' }];
  90. const live = [{ GeneralID: 1, dszt: 30, bxrq: '2026-09-02' }, { GeneralID: 2, dszt: 0, bxrq: '2026-09-26' }];
  91. const merged = context.merge(stored, live);
  92. assert.equal(merged.length, 2);
  93. assert.equal(merged.find((row) => row.GeneralID === 1).dszt, 30);
  94. assert.equal(merged.find((row) => row.GeneralID === 2).bxrq, '2026-09-26');
  95. });