| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import test from 'node:test';
- import assert from 'node:assert/strict';
- import vm from 'node:vm';
- import { readFileSync } from 'node:fs';
- const gatewaySource = readFileSync(new URL('../deploy-admin-functions.mjs', import.meta.url), 'utf8');
- const source = gatewaySource.match(/const LEGACY_REVIEW_PULLS=new Map\(\);[\s\S]*?\nconst APP_LIVE_SCOPES=/)?.[0]
- .replace(/\nconst APP_LIVE_SCOPES=[\s\S]*$/, '');
- assert.ok(source, '未找到旧系统抗遗忘记录拉取实现');
- function contextFor(rows) {
- let sequence = 0;
- let stored = [];
- const writes = [];
- const context = {
- console,
- URLSearchParams,
- AbortSignal,
- Map,
- Date,
- JSON,
- String,
- Math,
- Promise,
- DEFAULT_COMPANY_ID: 'company',
- number(value, fallback = 0) { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : fallback; },
- firstValue(...values) { return values.find((value) => value !== undefined && value !== null && value !== ''); },
- legacyLearningIso(value) { const parsed = new Date(String(value).replace(' ', 'T') + '+08:00'); return parsed.toISOString(); },
- randomObjectId() { sequence += 1; return `object${String(sequence).padStart(14, '0')}`; },
- async appLegacyConfig() { return { url: 'https://legacy.example/api', apiId: 'id', apiKey: 'key' }; },
- async fetch(url) {
- assert.match(String(url), /myfield2=yhid%3D42/);
- return { ok: true, async text() { return JSON.stringify({ retcode: 1, result: JSON.stringify(rows), page: { itemCount: rows.length, pageCount: 1 } }); } };
- },
- Psql: {
- async one(sql) {
- if (sql.includes('COUNT(*)') && sql.includes('MemoryPracticeRecord')) return { total: stored.length };
- throw new Error(`unexpected one sql: ${sql}`);
- },
- async query(sql) {
- if (sql.includes('SELECT c."generalId"')) return stored;
- if (sql.includes('SELECT CAST("id" AS text)')) return stored.map((row) => ({ id: row.itemId }));
- throw new Error(`unexpected query sql: ${sql}`);
- },
- },
- async mobileAtomicWrite(_input, _current, operations) {
- writes.push(operations);
- for (let index = 0; index < operations.length; index += 2) {
- const addon = operations[index].data;
- const common = operations[index + 1].data;
- stored = stored.filter((item) => Number(item.generalId) !== Number(common.generalId));
- stored.push({ ...common, ...addon, itemId: addon.id });
- }
- return operations.map((operation) => operation.data);
- },
- };
- vm.runInNewContext(`${source};this.normalize=legacyReviewRecord;this.sync=performLegacyReviewSync`, context);
- return { context, writes };
- }
- test('旧系统抗遗忘记录只接收当前学员并保留计划与完成状态', () => {
- const { context } = contextFor([]);
- assert.equal(context.normalize({ GeneralID: 1, yhid: 7 }, 42), null);
- const row = context.normalize({
- GeneralID: 866548,
- CreateTime: '2026-09-24 20:11:52',
- Title: '学员@21天抗遗忘@2026-09-24',
- yhid: 42,
- plid: 155,
- orderId: 859224,
- kcid: 171,
- kywsj: '2026-09-24 19:00',
- kywrq: '2026-09-24',
- xxjlid: 866545,
- fxzt: '1',
- wcsj: '2026-09-24 20:11',
- }, 42);
- assert.equal(row.generalId, 866548);
- assert.equal(row.addon.yhid, 42);
- assert.equal(row.addon.xxjlid, 866545);
- assert.equal(row.addon.fxzt, '1');
- assert.equal(row.addon.wcsj, '2026-09-24 20:11');
- });
- test('抗遗忘记录首次补齐后重复核对不再写入', async () => {
- const rows = [{
- GeneralID: 866548,
- CreateTime: '2026-09-24 20:11:52',
- Title: '学员@21天抗遗忘@2026-09-24',
- yhid: 42,
- plid: 155,
- orderId: 859224,
- kcid: 171,
- kywsj: '2026-09-24 19:00',
- kywrq: '2026-09-24',
- xxjlid: 866545,
- fxzt: '1',
- wcsj: '2026-09-24 20:11',
- }];
- const { context, writes } = contextFor(rows);
- const first = await context.sync(42, { id: 'actor' }, true);
- const second = await context.sync(42, { id: 'actor' }, true);
- assert.equal(first.written, 1);
- assert.equal(second.written, 0);
- assert.equal(writes.length, 1);
- assert.equal(writes[0][0].table, 'MemoryPracticeRecord');
- assert.equal(writes[0][1].match.generalId, 866548);
- });
|