coach-student-page.test.mjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { after, before, beforeEach, test } 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 source = readFileSync(new URL('../deploy-admin-functions.mjs', import.meta.url), 'utf8');
  8. const gateway = vm.createContext({});
  9. vm.runInContext(source.match(/const appGatewayCode = String.raw`([\s\S]*?)\n`;/)[1].replace(/const DEFAULT_COMPANY_ID = '[^']+';/, "const DEFAULT_COMPANY_ID = 'co';"), gateway);
  10. gateway.Psql = {
  11. one: async (sql, args) => (await db.query(sql, args)).rows[0],
  12. query: async (sql, args) => (await db.query(sql, args)).rows,
  13. };
  14. gateway.appOverviewToday = () => '2026-10-30';
  15. const coach = { get: key => key === 'legacyUserId' ? 300 : undefined };
  16. const page = (input = {}) => gateway.coachStudentPage({ scope: 'current-scheduled', ...input }, coach);
  17. const ids = result => Array.from(result.rows, row => row.UserID);
  18. before(async () => {
  19. await db.exec(`
  20. CREATE TABLE "_User"("objectId" text,"company" text,"legacyUserId" int,"legacyGroupId" int,"legacyUserData" jsonb,
  21. "isDisabled" boolean,"isDeleted" boolean,"username" text,"email" text,"mobile" text,"phone" text,"avatar" text,
  22. "nickname" text,"nickName" text,"realName" text,"createdAt" timestamptz);
  23. CREATE TABLE "CourseAppointment"("company" text,"id" int,"szyh" text,"pl" text,"dszt" text,"bxrq" text,"yysj" text,"isDeleted" boolean);
  24. CREATE TABLE "CommonModel"("company" text,"modelId" int,"itemId" int,"status" int,"updatedAt" timestamptz,"isDeleted" boolean);
  25. CREATE TABLE "DailyStudyRecord"("company" text,"id" int,"userId" numeric,"pl" text,"isDeleted" boolean);
  26. `);
  27. });
  28. beforeEach(() => db.exec('TRUNCATE "_User","CourseAppointment","CommonModel","DailyStudyRecord"'));
  29. after(() => db.close());
  30. async function user(id, { disabled = false, deleted = false, group = 1, company = 'co' } = {}) {
  31. await db.query('INSERT INTO "_User"("objectId","company","legacyUserId","legacyGroupId","isDisabled","isDeleted","username","legacyUserData") VALUES($1,$2,$3,$4,$5,$6,$7,$8)',
  32. ['u' + id, company, id, group, disabled, deleted, 'student-' + id, { GroupID: 1 }]);
  33. }
  34. async function appointment(id, student, { date = '2026-10-31', yysj = '', status = '0', commonStatus = 99, teacher = '300', company = 'co', deleted = false, commonDeleted = false } = {}) {
  35. await db.query('INSERT INTO "CourseAppointment" VALUES($1,$2,$3,$4,$5,$6,$7,$8)', [company, id, String(student), teacher, status, date, yysj, deleted]);
  36. await db.query('INSERT INTO "CommonModel" VALUES($1,54,$2,$3,$4,$5)', [company, id, commonStatus, '2026-10-30T00:00:00Z', commonDeleted]);
  37. }
  38. test('没有授课记录时仅列出正常账号且仍有今天或未来排课的学员,先筛选再分页', async () => {
  39. for (let id = 1; id <= 13; id++) await user(id, { disabled: id === 2, deleted: id === 3, group: id === 4 ? 3 : 1 });
  40. await appointment(1, 1);
  41. await appointment(2, 2);
  42. await appointment(3, 3);
  43. await appointment(4, 4);
  44. await appointment(5, 5, { date: '2026-10-29' });
  45. await appointment(6, 6, { status: '30' });
  46. await appointment(7, 7, { commonStatus: -2 });
  47. await appointment(8, 8, { teacher: '301' });
  48. await appointment(9, 9, { company: 'other' });
  49. await appointment(10, 10, { date: '2026-10-30', status: '10' });
  50. await appointment(11, 11, { deleted: true });
  51. await appointment(12, 12, { commonDeleted: true });
  52. await appointment(13, 13, { status: '-1' });
  53. await appointment(14, 999); // No matching account.
  54. await appointment(15, 1); // Multiple lessons must not duplicate a student.
  55. const first = await page({ psize: 1 });
  56. const second = await page({ psize: 1, cpage: 2 });
  57. assert.equal(first.page.itemCount, 2);
  58. assert.equal(first.page.pageCount, 2);
  59. assert.deepEqual([...ids(first), ...ids(second)].sort((a, b) => a - b), [1, 10]);
  60. });
  61. test('日期兼容紧凑日期、斜线日期及预约时间回退,空日期不进入列表', async () => {
  62. for (let id = 1; id <= 5; id++) await user(id);
  63. await appointment(1, 1, { date: '20261030' });
  64. await appointment(2, 2, { date: '', yysj: '2026-10-30 09:00:00' });
  65. await appointment(3, 3, { date: '2026/10/31' });
  66. await appointment(4, 4, { date: '', yysj: '' });
  67. await appointment(5, 5, { date: '20261029' });
  68. assert.deepEqual(ids(await page()).sort((a, b) => a - b), [1, 2, 3]);
  69. });
  70. test('有授课记录的正常学员在最后一节课结束后仍可查看历史,过滤停用、删除、取消及其他老师记录', async () => {
  71. for (let id = 1; id <= 9; id++) {
  72. await user(id, { disabled: id === 2, deleted: id === 3, group: id === 4 ? 3 : 1 });
  73. await appointment(id, id, { date: '2026-10-29', status: '30' });
  74. await db.query('INSERT INTO "DailyStudyRecord" VALUES($1,$2,$3,$4,$5)',
  75. [id === 5 ? 'other' : 'co', id, String(id), id === 6 ? '301' : '300', id === 7]);
  76. await db.query('INSERT INTO "CommonModel" VALUES($1,56,$2,$3,$4,$5)',
  77. [id === 5 ? 'other' : 'co', id, id === 8 ? -2 : 99, '2026-10-29T00:00:00Z', id === 9]);
  78. }
  79. await user(10); await appointment(10, 10); // Future lesson remains eligible before first study.
  80. await user(11); await appointment(11, 11, { date: '2026-10-29', status: '30' }); // No study record.
  81. const first = await page({ selectedStudentId: 1, psize: 1 });
  82. const second = await page({ selectedStudentId: 1, psize: 1, cpage: 2 });
  83. assert.equal(first.page.itemCount, 2);
  84. assert.deepEqual(ids(first), [1]);
  85. assert.deepEqual(ids(second), [10]);
  86. });
  87. test('恢复有效学员时将其放在首屏,停用或无排课的旧选择不能绕过筛选', async () => {
  88. for (let id = 1; id <= 4; id++) { await user(id, { disabled: id === 4 }); await appointment(id, id); }
  89. await user(5);
  90. const selected = await page({ selectedStudentId: 3, psize: 2 });
  91. assert.equal(ids(selected)[0], 3);
  92. const next = await page({ selectedStudentId: 3, psize: 2, cpage: 2 });
  93. assert.equal(new Set([...ids(selected), ...ids(next)]).size, 3);
  94. for (const selectedStudentId of [4, 5]) {
  95. const result = await page({ selectedStudentId });
  96. assert.equal(result.page.itemCount, 3);
  97. assert.ok(!ids(result).includes(selectedStudentId));
  98. }
  99. });
  100. test('筛选固定使用登录老师和当前帐套,请求参数不能指定其他老师', async () => {
  101. await user(1); await user(2); await user(3, { company: 'other' });
  102. await appointment(1, 1); await appointment(2, 2, { teacher: '301' }); await appointment(3, 3);
  103. assert.deepEqual(ids(await page({ uid: 301, coachId: 301, company: 'other' })), [1]);
  104. });
  105. test('未请求新筛选的我的学员接口仍可列出历史学员', async () => {
  106. await user(1); await user(2, { disabled: true });
  107. await appointment(1, 1, { date: '2020-01-01', status: '30' });
  108. await appointment(2, 2);
  109. assert.deepEqual(ids(await page()), []);
  110. assert.deepEqual(ids(await page({ scope: '' })).sort((a, b) => a - b), [1, 2]);
  111. });