legacy-review-pull.test.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_REVIEW_PULLS=new Map\(\);[\s\S]*?\nconst APP_LIVE_SCOPES=/)?.[0]
  7. .replace(/\nconst APP_LIVE_SCOPES=[\s\S]*$/, '');
  8. assert.ok(source, '未找到旧系统抗遗忘记录拉取实现');
  9. function contextFor(rows) {
  10. let sequence = 0;
  11. let stored = [];
  12. const writes = [];
  13. const context = {
  14. console,
  15. URLSearchParams,
  16. AbortSignal,
  17. Map,
  18. Date,
  19. JSON,
  20. String,
  21. Math,
  22. Promise,
  23. DEFAULT_COMPANY_ID: 'company',
  24. number(value, fallback = 0) { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : fallback; },
  25. firstValue(...values) { return values.find((value) => value !== undefined && value !== null && value !== ''); },
  26. legacyLearningIso(value) { const parsed = new Date(String(value).replace(' ', 'T') + '+08:00'); return parsed.toISOString(); },
  27. randomObjectId() { sequence += 1; return `object${String(sequence).padStart(14, '0')}`; },
  28. async appLegacyConfig() { return { url: 'https://legacy.example/api', apiId: 'id', apiKey: 'key' }; },
  29. async fetch(url) {
  30. assert.match(String(url), /myfield2=yhid%3D42/);
  31. return { ok: true, async text() { return JSON.stringify({ retcode: 1, result: JSON.stringify(rows), page: { itemCount: rows.length, pageCount: 1 } }); } };
  32. },
  33. Psql: {
  34. async one(sql) {
  35. if (sql.includes('COUNT(*)') && sql.includes('MemoryPracticeRecord')) return { total: stored.length };
  36. throw new Error(`unexpected one sql: ${sql}`);
  37. },
  38. async query(sql) {
  39. if (sql.includes('SELECT c."generalId"')) return stored;
  40. if (sql.includes('SELECT CAST("id" AS text)')) return stored.map((row) => ({ id: row.itemId }));
  41. throw new Error(`unexpected query sql: ${sql}`);
  42. },
  43. },
  44. async mobileAtomicWrite(_input, _current, operations) {
  45. writes.push(operations);
  46. for (let index = 0; index < operations.length; index += 2) {
  47. const addon = operations[index].data;
  48. const common = operations[index + 1].data;
  49. stored = stored.filter((item) => Number(item.generalId) !== Number(common.generalId));
  50. stored.push({ ...common, ...addon, itemId: addon.id });
  51. }
  52. return operations.map((operation) => operation.data);
  53. },
  54. };
  55. vm.runInNewContext(`${source};this.normalize=legacyReviewRecord;this.sync=performLegacyReviewSync`, context);
  56. return { context, writes };
  57. }
  58. test('旧系统抗遗忘记录只接收当前学员并保留计划与完成状态', () => {
  59. const { context } = contextFor([]);
  60. assert.equal(context.normalize({ GeneralID: 1, yhid: 7 }, 42), null);
  61. const row = context.normalize({
  62. GeneralID: 866548,
  63. CreateTime: '2026-09-24 20:11:52',
  64. Title: '学员@21天抗遗忘@2026-09-24',
  65. yhid: 42,
  66. plid: 155,
  67. orderId: 859224,
  68. kcid: 171,
  69. kywsj: '2026-09-24 19:00',
  70. kywrq: '2026-09-24',
  71. xxjlid: 866545,
  72. fxzt: '1',
  73. wcsj: '2026-09-24 20:11',
  74. }, 42);
  75. assert.equal(row.generalId, 866548);
  76. assert.equal(row.addon.yhid, 42);
  77. assert.equal(row.addon.xxjlid, 866545);
  78. assert.equal(row.addon.fxzt, '1');
  79. assert.equal(row.addon.wcsj, '2026-09-24 20:11');
  80. });
  81. test('抗遗忘记录首次补齐后重复核对不再写入', async () => {
  82. const rows = [{
  83. GeneralID: 866548,
  84. CreateTime: '2026-09-24 20:11:52',
  85. Title: '学员@21天抗遗忘@2026-09-24',
  86. yhid: 42,
  87. plid: 155,
  88. orderId: 859224,
  89. kcid: 171,
  90. kywsj: '2026-09-24 19:00',
  91. kywrq: '2026-09-24',
  92. xxjlid: 866545,
  93. fxzt: '1',
  94. wcsj: '2026-09-24 20:11',
  95. }];
  96. const { context, writes } = contextFor(rows);
  97. const first = await context.sync(42, { id: 'actor' }, true);
  98. const second = await context.sync(42, { id: 'actor' }, true);
  99. assert.equal(first.written, 1);
  100. assert.equal(second.written, 0);
  101. assert.equal(writes.length, 1);
  102. assert.equal(writes[0][0].table, 'MemoryPracticeRecord');
  103. assert.equal(writes[0][1].match.generalId, 866548);
  104. });