teaching-course-mapping.test.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { test, before, after } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { readFileSync } from 'node:fs';
  4. import vm from 'node:vm';
  5. import { PGlite } from '@electric-sql/pglite';
  6. const db = new PGlite();
  7. const context = { Psql: { query: async (sql, values) => (await db.query(sql, values)).rows } };
  8. vm.createContext(context);
  9. const source = readFileSync(new URL('../deploy-admin-functions.mjs', import.meta.url), 'utf8');
  10. vm.runInContext(source.match(/const operationsGatewayCode = String.raw`([\s\S]*?)\n`;/)[1], context);
  11. const profile = (id, categoryKey) => ({ id: String(id), get: key => ({ courseId: id, categoryKey, confirmed: true })[key] });
  12. context.companyIdOf = () => 'company-a';
  13. context.companyPointer = () => 'company-a';
  14. context.requireRole = () => {};
  15. context.reasonOf = () => 'mapping test';
  16. context.newClassObjects = async name => name === 'CourseTeachingProfile' ? [profile(454, 'word'), profile(464, 'word'), profile(398, 'word')] : [];
  17. before(async () => {
  18. await db.exec(`
  19. CREATE TABLE "Node" (company text,"nodeId" numeric,"parentId" numeric,"nodeName" text,zstatus numeric);
  20. CREATE TABLE "_User" (company text,"legacyUserId" numeric,"isDisabled" boolean,"isDeleted" boolean);
  21. CREATE TABLE "CourseBinding" ("objectId" text,company text,id numeric,yhid text,kcid text,"categoryKey" text,"categoryName" text,"enrollmentStatus" text,"updatedAt" timestamptz);
  22. CREATE TABLE "CourseAppointment" (company text,id numeric,kcid text);
  23. CREATE TABLE "CommonModel" ("objectId" text,company text,"modelId" numeric,"itemId" numeric,status numeric,subtitle text);
  24. INSERT INTO "Node" VALUES ('company-a',8,0,'小学',99),('company-a',81,8,'小学语法写作课',99),('company-b',464,0,'其他帐套课程',99);
  25. INSERT INTO "_User" VALUES ('company-a',20,false,false);
  26. INSERT INTO "CourseBinding" ("objectId",company,id,yhid,kcid,"enrollmentStatus") VALUES ('binding-81','company-a',81,20,'81','pending'),('binding-active','company-a',82,99,'81','active');
  27. INSERT INTO "CourseAppointment" VALUES ('company-a',1,'398'),('company-a',2,'454'),('company-a',3,'464'),('company-a',4,'999'),('company-b',5,'455');
  28. INSERT INTO "CommonModel" VALUES ('appointment-1','company-a',54,1,99,'阅读写作'),('appointment-2','company-a',54,2,99,'牛津上海版初中新课标'),('appointment-3','company-a',54,3,99,'体育单招1700词'),('appointment-4','company-a',54,4,-2,'已取消的缺失课程'),('appointment-5','company-b',54,5,99,'其他帐套课程'),('binding-common','company-a',58,81,99,'');
  29. `);
  30. });
  31. after(() => db.close());
  32. test('writing maps only to its explicit school stage; generic writing is never suggested as word lessons', () => {
  33. for (const stage of ['primary', 'middle', 'high']) assert.equal(context.suggestedTeachingCategory('阅读写作', stage), stage + '_writing');
  34. assert.equal(context.suggestedTeachingCategory('语法写作课', ''), '');
  35. for (const name of ['牛津上海版初中新课标', '体育单招1700词']) assert.equal(context.suggestedTeachingCategory(name, ''), 'word');
  36. });
  37. test('real course query recovers missing Node courses, preserves tenant isolation and stage checks', async () => {
  38. const rows = await context.courseTeachingRows({}, {});
  39. const byId = new Map(rows.map(row => [row.courseId, row]));
  40. assert.equal(byId.get(81).stageKey, 'primary');
  41. assert.equal(byId.get(81).suggestedCategoryKey, 'primary_writing');
  42. assert.equal(byId.get(454).stageKey, 'middle');
  43. assert.equal(byId.get(454).confirmed, true);
  44. assert.equal(byId.get(464).courseName, '体育单招1700词');
  45. assert.equal(byId.get(464).confirmed, true);
  46. assert.equal(byId.get(398).confirmed, false);
  47. assert.equal(byId.get(398).suggestedCategoryKey, '');
  48. assert.equal(byId.has(999), false);
  49. assert.equal(byId.has(455), false);
  50. });
  51. test('mapping rejects cross-stage writing, unknown courses and generic writing before saving', async () => {
  52. await assert.rejects(context.commitCourseMappings({}, {}, { items: [{ courseId: 81, categoryKey: 'high_writing' }] }), /语法写作分类必须/);
  53. await assert.rejects(context.commitCourseMappings({}, {}, { items: [{ courseId: 398, categoryKey: 'word' }] }), /语法写作课程缺少学段/);
  54. await assert.rejects(context.commitCourseMappings({}, {}, { items: [{ courseId: 999, categoryKey: 'word' }] }), /未找到该课程/);
  55. });
  56. test('confirmed course mapping activates a valid unambiguous historical binding', async () => {
  57. const rows = await context.syncConfirmedCourseBindings({}, {}, 81, 'primary_writing', '小学语法写作课');
  58. assert.deepEqual(rows.map(row => row.enrollmentStatus), ['active']);
  59. const binding = (await db.query('SELECT "categoryKey","categoryName","enrollmentStatus" FROM "CourseBinding" WHERE "objectId"=\'binding-81\'')).rows[0];
  60. assert.deepEqual(binding, { categoryKey: 'primary_writing', categoryName: '小学语法写作课', enrollmentStatus: 'active' });
  61. const preserved = (await db.query('SELECT "categoryKey","enrollmentStatus" FROM "CourseBinding" WHERE "objectId"=\'binding-active\'')).rows[0];
  62. assert.deepEqual(preserved, { categoryKey: null, enrollmentStatus: 'active' });
  63. });