coach-student-page.test.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. `);
  26. });
  27. beforeEach(() => db.exec('TRUNCATE "_User","CourseAppointment","CommonModel"'));
  28. after(() => db.close());
  29. async function user(id, { disabled = false, deleted = false, group = 1, company = 'co' } = {}) {
  30. await db.query('INSERT INTO "_User"("objectId","company","legacyUserId","legacyGroupId","isDisabled","isDeleted","username","legacyUserData") VALUES($1,$2,$3,$4,$5,$6,$7,$8)',
  31. ['u' + id, company, id, group, disabled, deleted, 'student-' + id, { GroupID: 1 }]);
  32. }
  33. async function appointment(id, student, { date = '2026-10-31', yysj = '', status = '0', commonStatus = 99, teacher = '300', company = 'co', deleted = false, commonDeleted = false } = {}) {
  34. await db.query('INSERT INTO "CourseAppointment" VALUES($1,$2,$3,$4,$5,$6,$7,$8)', [company, id, String(student), teacher, status, date, yysj, deleted]);
  35. await db.query('INSERT INTO "CommonModel" VALUES($1,54,$2,$3,$4,$5)', [company, id, commonStatus, '2026-10-30T00:00:00Z', commonDeleted]);
  36. }
  37. test('仅列出正常学员账号且在当前老师名下仍有今天或未来排课,先筛选再分页', async () => {
  38. for (let id = 1; id <= 13; id++) await user(id, { disabled: id === 2, deleted: id === 3, group: id === 4 ? 3 : 1 });
  39. await appointment(1, 1);
  40. await appointment(2, 2);
  41. await appointment(3, 3);
  42. await appointment(4, 4);
  43. await appointment(5, 5, { date: '2026-10-29' });
  44. await appointment(6, 6, { status: '30' });
  45. await appointment(7, 7, { commonStatus: -2 });
  46. await appointment(8, 8, { teacher: '301' });
  47. await appointment(9, 9, { company: 'other' });
  48. await appointment(10, 10, { date: '2026-10-30', status: '10' });
  49. await appointment(11, 11, { deleted: true });
  50. await appointment(12, 12, { commonDeleted: true });
  51. await appointment(13, 13, { status: '-1' });
  52. await appointment(14, 999); // No matching account.
  53. await appointment(15, 1); // Multiple lessons must not duplicate a student.
  54. const first = await page({ psize: 1 });
  55. const second = await page({ psize: 1, cpage: 2 });
  56. assert.equal(first.page.itemCount, 2);
  57. assert.equal(first.page.pageCount, 2);
  58. assert.deepEqual([...ids(first), ...ids(second)].sort((a, b) => a - b), [1, 10]);
  59. });
  60. test('日期兼容紧凑日期、斜线日期及预约时间回退,空日期不进入列表', async () => {
  61. for (let id = 1; id <= 5; id++) await user(id);
  62. await appointment(1, 1, { date: '20261030' });
  63. await appointment(2, 2, { date: '', yysj: '2026-10-30 09:00:00' });
  64. await appointment(3, 3, { date: '2026/10/31' });
  65. await appointment(4, 4, { date: '', yysj: '' });
  66. await appointment(5, 5, { date: '20261029' });
  67. assert.deepEqual(ids(await page()).sort((a, b) => a - b), [1, 2, 3]);
  68. });
  69. test('恢复有效学员时将其放在首屏,停用或无排课的旧选择不能绕过筛选', async () => {
  70. for (let id = 1; id <= 4; id++) { await user(id, { disabled: id === 4 }); await appointment(id, id); }
  71. await user(5);
  72. const selected = await page({ selectedStudentId: 3, psize: 2 });
  73. assert.equal(ids(selected)[0], 3);
  74. const next = await page({ selectedStudentId: 3, psize: 2, cpage: 2 });
  75. assert.equal(new Set([...ids(selected), ...ids(next)]).size, 3);
  76. for (const selectedStudentId of [4, 5]) {
  77. const result = await page({ selectedStudentId });
  78. assert.equal(result.page.itemCount, 3);
  79. assert.ok(!ids(result).includes(selectedStudentId));
  80. }
  81. });
  82. test('筛选固定使用登录老师和当前帐套,请求参数不能指定其他老师', async () => {
  83. await user(1); await user(2); await user(3, { company: 'other' });
  84. await appointment(1, 1); await appointment(2, 2, { teacher: '301' }); await appointment(3, 3);
  85. assert.deepEqual(ids(await page({ uid: 301, coachId: 301, company: 'other' })), [1]);
  86. });
  87. test('未请求新筛选的我的学员接口仍可列出历史学员', async () => {
  88. await user(1); await user(2, { disabled: true });
  89. await appointment(1, 1, { date: '2020-01-01', status: '30' });
  90. await appointment(2, 2);
  91. assert.deepEqual(ids(await page()), []);
  92. assert.deepEqual(ids(await page({ scope: '' })).sort((a, b) => a - b), [1, 2]);
  93. });