legacy-review-pull.test.mjs 4.8 KB

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