@quad-education/sdk 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contract-tests/apps.js +5 -2
- package/dist/contract-tests/suite.js +75 -62
- package/dist/src/contract/recipients.d.ts +1 -1
- package/dist/src/contract/recipients.js +1 -1
- package/dist/src/contract/types.d.ts +2 -2
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/engine.d.ts +12 -3
- package/dist/src/engine/engine.js +21 -3
- package/dist/src/testing/index.d.ts +9 -4
- package/dist/src/testing/index.js +10 -13
- package/package.json +56 -45
|
@@ -248,10 +248,13 @@ export const TEST_JOBS = [
|
|
|
248
248
|
name: 'weekly',
|
|
249
249
|
async run(ctx) {
|
|
250
250
|
await logRun(ctx);
|
|
251
|
-
|
|
251
|
+
// D129: the Year 9 year group's staff, found by kind and year level, never by a code.
|
|
252
|
+
const { items } = await ctx.sdk.school.cohorts.list({
|
|
253
|
+
where: { kind: 'year', yearLevel: 'Y9' },
|
|
254
|
+
});
|
|
252
255
|
await ctx.sdk.notify.send({
|
|
253
256
|
type: 'test.message',
|
|
254
|
-
to: staffOf(
|
|
257
|
+
to: staffOf(items[0].id),
|
|
255
258
|
subject: 'Weekly summary',
|
|
256
259
|
body: 'Summary',
|
|
257
260
|
idempotencyKey: `notify:${ctx.runId}`,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any -- the suite drives the SDK untyped, like plain JS would */
|
|
2
2
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
3
3
|
import { eligibleParents } from '@quad-education/canonical';
|
|
4
|
-
import {
|
|
4
|
+
import { suggestedPeople, withDerived } from '@quad-education/fictional-school';
|
|
5
5
|
import stored from '@quad-education/fictional-school/dataset' with { type: 'json' };
|
|
6
6
|
import { parentsOf, staffOf, studentsOf } from "../src/index.js";
|
|
7
7
|
import { ACCESS_APP, BASIC_APP, JOBS_APP, ROLES_APP, TEST_APP, MEDICAL_APP, slowGate, } from "./apps.js";
|
|
@@ -9,13 +9,22 @@ import { expectError, ids, listAll } from "./helpers.js";
|
|
|
9
9
|
// The fictional school is the oracle: tests compare what the SDK returns with what the
|
|
10
10
|
// dataset says should be visible. They never read an implementation's storage.
|
|
11
11
|
const ds = withDerived(stored);
|
|
12
|
-
const personas =
|
|
13
|
-
const persona = (
|
|
14
|
-
const p = personas.find((x) => x.
|
|
12
|
+
const personas = suggestedPeople(ds);
|
|
13
|
+
const persona = (rule) => {
|
|
14
|
+
const p = personas.find((x) => x.rule === rule);
|
|
15
15
|
return { kind: 'user', role: p.role, personId: p.personId };
|
|
16
16
|
};
|
|
17
17
|
const STAFF = persona('primary-teacher');
|
|
18
|
-
const
|
|
18
|
+
const SUPERVISOR = persona('year-group-staff');
|
|
19
|
+
// D129: a school's system names no positions, so an app routes to people its staff chose. These two stand in for them.
|
|
20
|
+
const APPROVERS = [persona('secondary-teacher').personId, SUPERVISOR.personId].sort();
|
|
21
|
+
const approvers = APPROVERS.map((person) => ({ person }));
|
|
22
|
+
// Cohorts are found by kind, year level and subject, never by a code (D129).
|
|
23
|
+
const cohortWhere = (match) => ds.cohorts.find((c) => Object.entries(match).every(([k, v]) => c[k] === v));
|
|
24
|
+
const staffIn = (cohortId) => ds.cohortMemberships
|
|
25
|
+
.filter((m) => m.cohortId === cohortId && m.memberType === 'staff')
|
|
26
|
+
.map((m) => m.personId)
|
|
27
|
+
.sort();
|
|
19
28
|
const STUDENT = persona('student');
|
|
20
29
|
const SIBLINGS_PARENT = persona('parent-of-siblings');
|
|
21
30
|
const SEPARATED_PARENT = persona('separated-parent');
|
|
@@ -52,7 +61,7 @@ export function defineContractSuite(name, factory) {
|
|
|
52
61
|
},
|
|
53
62
|
});
|
|
54
63
|
expect(sdk.appId).toBe(TEST_APP.manifest.id);
|
|
55
|
-
expect(sdk.version).toBe('0.
|
|
64
|
+
expect(sdk.version).toBe('0.5.0');
|
|
56
65
|
});
|
|
57
66
|
it('knows parents and students', async () => {
|
|
58
67
|
expect((await (await env.connect(STUDENT)).auth.me()).role).toBe('student');
|
|
@@ -232,14 +241,15 @@ export function defineContractSuite(name, factory) {
|
|
|
232
241
|
await expectError(sdk.school.students.list({ where: { shoeSize: 9 } }), 'VALIDATION');
|
|
233
242
|
await expectError(sdk.school.students.list({ cursor: 'not-a-cursor' }), 'VALIDATION');
|
|
234
243
|
});
|
|
235
|
-
it(
|
|
244
|
+
it("finds a cohort by the school's own code, and lists its staff beside its students (D129)", async () => {
|
|
236
245
|
const sdk = await env.connect(STAFF);
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
expect(members.
|
|
242
|
-
expect(
|
|
246
|
+
const year9 = cohortWhere({ kind: 'year', yearLevel: 'Y9' });
|
|
247
|
+
const found = await sdk.school.cohorts.byCode(year9.code);
|
|
248
|
+
expect(found).toMatchObject({ id: year9.id, kind: 'year' });
|
|
249
|
+
const members = await sdk.school.cohorts.members(found.id);
|
|
250
|
+
expect(members.filter((m) => m.memberType === 'staff').map((m) => m.personId)).toEqual([SUPERVISOR.personId]);
|
|
251
|
+
expect(members.some((m) => m.memberType === 'student')).toBe(true);
|
|
252
|
+
expect(await sdk.school.cohorts.byCode('no-such-code')).toBeNull();
|
|
243
253
|
});
|
|
244
254
|
it('lists a student’s parents and a parent’s children within visibility', async () => {
|
|
245
255
|
const staff = await env.connect(STAFF);
|
|
@@ -252,7 +262,12 @@ export function defineContractSuite(name, factory) {
|
|
|
252
262
|
await expectError(student.school.parents.children(parentId), 'NOT_FOUND');
|
|
253
263
|
});
|
|
254
264
|
it('serves the timetable, which is derived, like any other entity (S4.1)', async () => {
|
|
255
|
-
const english =
|
|
265
|
+
const english = cohortWhere({
|
|
266
|
+
kind: 'class',
|
|
267
|
+
yearLevel: 'Y9',
|
|
268
|
+
subject: 'English',
|
|
269
|
+
name: 'English 9B',
|
|
270
|
+
});
|
|
256
271
|
const week = { gte: '2026-03-02', lt: '2026-03-09' };
|
|
257
272
|
const expected = ds.lessons
|
|
258
273
|
.filter((l) => l.cohortId === english.id && l.date >= week.gte && l.date < week.lt)
|
|
@@ -430,13 +445,15 @@ export function defineContractSuite(name, factory) {
|
|
|
430
445
|
expect('note' in created).toBe(false);
|
|
431
446
|
expect(await greetings.get(created.id)).toEqual(created);
|
|
432
447
|
await env.control.setClock('2026-03-05T03:00:00.000Z');
|
|
433
|
-
const
|
|
434
|
-
const updated = await
|
|
448
|
+
const supervisor = await env.connect(SUPERVISOR);
|
|
449
|
+
const updated = await supervisor.db
|
|
450
|
+
.entity('greeting')
|
|
451
|
+
.update(created.id, { tone: 'formal' });
|
|
435
452
|
expect(updated).toMatchObject({
|
|
436
453
|
tone: 'formal',
|
|
437
454
|
message: 'Hello',
|
|
438
455
|
createdBy: STAFF.personId,
|
|
439
|
-
updatedBy:
|
|
456
|
+
updatedBy: SUPERVISOR.personId,
|
|
440
457
|
updatedAt: '2026-03-05T03:00:00.000Z',
|
|
441
458
|
});
|
|
442
459
|
await greetings.delete(created.id);
|
|
@@ -765,16 +782,20 @@ export function defineContractSuite(name, factory) {
|
|
|
765
782
|
const message = { type: 'test.message', subject: 'Hello', body: 'Body text' };
|
|
766
783
|
it('delivers to the simulated outbox only', async () => {
|
|
767
784
|
const sdk = await env.connect(STAFF);
|
|
768
|
-
const
|
|
769
|
-
const result = await sdk.notify.send({
|
|
770
|
-
|
|
785
|
+
const supervisorId = SUPERVISOR.personId;
|
|
786
|
+
const result = await sdk.notify.send({
|
|
787
|
+
...message,
|
|
788
|
+
to: { person: supervisorId },
|
|
789
|
+
link: '/x',
|
|
790
|
+
});
|
|
791
|
+
expect(result.recipients).toEqual([{ personId: supervisorId, role: 'staff' }]);
|
|
771
792
|
const outbox = await env.control.outbox();
|
|
772
793
|
expect(outbox).toHaveLength(1);
|
|
773
794
|
expect(outbox[0]).toMatchObject({
|
|
774
795
|
appId: TEST_APP.manifest.id,
|
|
775
796
|
type: 'test.message',
|
|
776
797
|
channel: 'inApp',
|
|
777
|
-
recipientId:
|
|
798
|
+
recipientId: supervisorId,
|
|
778
799
|
recipientRole: 'staff',
|
|
779
800
|
sentBy: STAFF.personId,
|
|
780
801
|
simulated: true,
|
|
@@ -786,19 +807,20 @@ export function defineContractSuite(name, factory) {
|
|
|
786
807
|
await expectError(sdk.notify.send({ ...message, type: 'test.undeclared', to: { role: 'staff' } }), 'UNDECLARED_TYPE');
|
|
787
808
|
expect(await env.control.outbox()).toEqual([]);
|
|
788
809
|
});
|
|
789
|
-
it(
|
|
810
|
+
it("routes to a cohort's staff with staffOf (D129)", async () => {
|
|
790
811
|
const sdk = await env.connect(STAFF);
|
|
791
|
-
const
|
|
792
|
-
const
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
const
|
|
797
|
-
|
|
812
|
+
const year9 = cohortWhere({ kind: 'year', yearLevel: 'Y9' });
|
|
813
|
+
const result = await sdk.notify.send({ ...message, to: staffOf(year9.id) });
|
|
814
|
+
expect(result.recipients.map((r) => r.personId).sort()).toEqual(staffIn(year9.id));
|
|
815
|
+
});
|
|
816
|
+
it('routes to people the app chose, as an array of person recipients', async () => {
|
|
817
|
+
const sdk = await env.connect(STAFF);
|
|
818
|
+
const result = await sdk.notify.send({ ...message, to: approvers });
|
|
819
|
+
expect(result.recipients.map((r) => r.personId).sort()).toEqual(APPROVERS);
|
|
798
820
|
});
|
|
799
821
|
it('routes to students of a cohort', async () => {
|
|
800
822
|
const sdk = await env.connect(STAFF);
|
|
801
|
-
const cls =
|
|
823
|
+
const cls = cohortWhere({ kind: 'class', yearLevel: 'Y3', name: '3A' });
|
|
802
824
|
const expected = ds.cohortMemberships
|
|
803
825
|
.filter((m) => m.cohortId === cls.id && m.memberType === 'student')
|
|
804
826
|
.map((m) => m.personId)
|
|
@@ -859,7 +881,7 @@ export function defineContractSuite(name, factory) {
|
|
|
859
881
|
});
|
|
860
882
|
it('drops repeats with the same idempotency key', async () => {
|
|
861
883
|
const sdk = await env.connect(STAFF);
|
|
862
|
-
const send = () => sdk.notify.send({ ...message, to: { person:
|
|
884
|
+
const send = () => sdk.notify.send({ ...message, to: { person: SUPERVISOR.personId }, idempotencyKey: 'n1' });
|
|
863
885
|
await send();
|
|
864
886
|
await send();
|
|
865
887
|
expect(await env.control.outbox()).toHaveLength(1);
|
|
@@ -897,38 +919,29 @@ export function defineContractSuite(name, factory) {
|
|
|
897
919
|
// ---------------- Slice B ----------------
|
|
898
920
|
const HOUR = 3_600_000;
|
|
899
921
|
const DAY = 24 * HOUR;
|
|
900
|
-
const teamMembers = (code) => {
|
|
901
|
-
const cohort = ds.cohorts.find((c) => c.code === code);
|
|
902
|
-
return ds.cohortMemberships
|
|
903
|
-
.filter((m) => m.cohortId === cohort.id)
|
|
904
|
-
.map((m) => m.personId)
|
|
905
|
-
.sort();
|
|
906
|
-
};
|
|
907
922
|
const SYSTEM_JOBS = { kind: 'system', appId: 'contract-jobs' };
|
|
908
923
|
const approval = { type: 'test.approval', title: 'Approve the excursion' };
|
|
909
924
|
describe('tasks (D6)', () => {
|
|
910
925
|
useApp(JOBS_APP);
|
|
911
926
|
it("mode 'each' gives every assignee their own task and inbox entry", async () => {
|
|
912
927
|
const sdk = await env.connect(STAFF);
|
|
913
|
-
const
|
|
914
|
-
const
|
|
915
|
-
|
|
916
|
-
expect(tasks).toHaveLength(deputies.length);
|
|
928
|
+
const { tasks } = await sdk.tasks.assign({ ...approval, assignee: approvers });
|
|
929
|
+
const assignees = APPROVERS;
|
|
930
|
+
expect(tasks).toHaveLength(assignees.length);
|
|
917
931
|
expect(new Set(tasks.map((t) => t.groupId)).size).toBe(1);
|
|
918
|
-
expect(tasks.flatMap((t) => t.assignees).sort()).toEqual(
|
|
919
|
-
for (const id of
|
|
932
|
+
expect(tasks.flatMap((t) => t.assignees).sort()).toEqual(assignees);
|
|
933
|
+
for (const id of assignees)
|
|
920
934
|
expect(await env.control.inbox(id)).toHaveLength(1);
|
|
921
935
|
});
|
|
922
936
|
it("mode 'any' gives one shared task that closes when the first assignee completes it", async () => {
|
|
923
937
|
const sdk = await env.connect(STAFF);
|
|
924
|
-
const team = await sdk.school.cohorts.byCode('deputy-principal');
|
|
925
938
|
const { tasks } = await sdk.tasks.assign({
|
|
926
939
|
...approval,
|
|
927
|
-
assignee:
|
|
940
|
+
assignee: approvers,
|
|
928
941
|
mode: 'any',
|
|
929
942
|
});
|
|
930
943
|
expect(tasks).toHaveLength(1);
|
|
931
|
-
const [first, second] =
|
|
944
|
+
const [first, second] = APPROVERS;
|
|
932
945
|
const done = await (await env.connect({ kind: 'user', role: 'staff', personId: first })).tasks.complete(tasks[0].id, {
|
|
933
946
|
outcome: 'approved',
|
|
934
947
|
});
|
|
@@ -936,13 +949,13 @@ export function defineContractSuite(name, factory) {
|
|
|
936
949
|
expect(await env.control.inbox(second)).toEqual([]);
|
|
937
950
|
await expectError((await env.connect({ kind: 'user', role: 'staff', personId: second })).tasks.complete(tasks[0].id), 'VALIDATION');
|
|
938
951
|
});
|
|
939
|
-
it(
|
|
952
|
+
it("an approval routed to a year group's staff reaches only them (D129)", async () => {
|
|
940
953
|
const sdk = await env.connect(STAFF);
|
|
941
|
-
const
|
|
942
|
-
await sdk.tasks.assign({ ...approval, assignee: staffOf(
|
|
943
|
-
const
|
|
954
|
+
const year9 = cohortWhere({ kind: 'year', yearLevel: 'Y9' });
|
|
955
|
+
await sdk.tasks.assign({ ...approval, assignee: staffOf(year9.id), mode: 'any' });
|
|
956
|
+
const assignees = new Set(staffIn(year9.id));
|
|
944
957
|
for (const s of ds.staff) {
|
|
945
|
-
expect((await env.control.inbox(s.id)).length, s.id).toBe(
|
|
958
|
+
expect((await env.control.inbox(s.id)).length, s.id).toBe(assignees.has(s.id) ? 1 : 0);
|
|
946
959
|
}
|
|
947
960
|
});
|
|
948
961
|
it('permission slips never reach a restricted or non-consenting parent (D17)', async () => {
|
|
@@ -1029,22 +1042,22 @@ export function defineContractSuite(name, factory) {
|
|
|
1029
1042
|
const sdk = await env.connect(STAFF);
|
|
1030
1043
|
const first = await sdk.tasks.assign({
|
|
1031
1044
|
...approval,
|
|
1032
|
-
assignee: { person:
|
|
1045
|
+
assignee: { person: SUPERVISOR.personId },
|
|
1033
1046
|
idempotencyKey: 'once',
|
|
1034
1047
|
});
|
|
1035
1048
|
const again = await sdk.tasks.assign({
|
|
1036
1049
|
...approval,
|
|
1037
|
-
assignee: { person:
|
|
1050
|
+
assignee: { person: SUPERVISOR.personId },
|
|
1038
1051
|
idempotencyKey: 'once',
|
|
1039
1052
|
});
|
|
1040
1053
|
expect(again.tasks.map((t) => t.id)).toEqual(first.tasks.map((t) => t.id));
|
|
1041
1054
|
await env.control.restart();
|
|
1042
|
-
expect(await env.control.inbox(
|
|
1055
|
+
expect(await env.control.inbox(SUPERVISOR.personId)).toHaveLength(1);
|
|
1043
1056
|
expect((await (await env.connect(STAFF)).tasks.get(first.tasks[0].id)).title).toBe(approval.title);
|
|
1044
1057
|
});
|
|
1045
1058
|
it('a task about a student goes only to people who can see that student (D25)', async () => {
|
|
1046
1059
|
const staff = await env.connect(STAFF);
|
|
1047
|
-
const cls =
|
|
1060
|
+
const cls = cohortWhere({ kind: 'class', yearLevel: 'Y3', name: '3A' });
|
|
1048
1061
|
const classmates = ds.cohortMemberships
|
|
1049
1062
|
.filter((m) => m.cohortId === cls.id && m.memberType === 'student')
|
|
1050
1063
|
.map((m) => m.personId);
|
|
@@ -1207,7 +1220,7 @@ export function defineContractSuite(name, factory) {
|
|
|
1207
1220
|
},
|
|
1208
1221
|
]);
|
|
1209
1222
|
expect(await env.control.runDueJobs()).toEqual([]);
|
|
1210
|
-
expect((await env.control.outbox()).map((e) => e.recipientId).sort()).toEqual(
|
|
1223
|
+
expect((await env.control.outbox()).map((e) => e.recipientId).sort()).toEqual(staffIn(cohortWhere({ kind: 'year', yearLevel: 'Y9' }).id));
|
|
1211
1224
|
// After DST ends (5 April), Monday 07:00 AEST is 21:00 UTC.
|
|
1212
1225
|
expect((await sdk.cron.list())[0].nextRunAt).toBe('2026-03-29T20:00:00.000Z');
|
|
1213
1226
|
await env.control.advanceClock(14 * DAY);
|
|
@@ -1310,12 +1323,12 @@ export function defineContractSuite(name, factory) {
|
|
|
1310
1323
|
const staff = await env.connect(STAFF);
|
|
1311
1324
|
const { tasks } = await staff.tasks.assign({
|
|
1312
1325
|
...approval,
|
|
1313
|
-
assignee: { person:
|
|
1326
|
+
assignee: { person: SUPERVISOR.personId },
|
|
1314
1327
|
});
|
|
1315
|
-
await (await env.connect(
|
|
1328
|
+
await (await env.connect(SUPERVISOR)).tasks.complete(tasks[0].id);
|
|
1316
1329
|
const other = await staff.tasks.assign({
|
|
1317
1330
|
...approval,
|
|
1318
|
-
assignee: { person:
|
|
1331
|
+
assignee: { person: SUPERVISOR.personId },
|
|
1319
1332
|
});
|
|
1320
1333
|
await staff.tasks.cancel(other.tasks[0].id);
|
|
1321
1334
|
const restricted = ds.parentStudentLinks.find((l) => l.contactRestricted);
|
|
@@ -1358,10 +1371,10 @@ export function defineContractSuite(name, factory) {
|
|
|
1358
1371
|
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
1359
1372
|
const staff = await env.connect(STAFF);
|
|
1360
1373
|
await staff.school.medicalAlerts.list({ limit: 1 });
|
|
1361
|
-
await staff.tasks.assign({ ...approval, assignee: { person:
|
|
1374
|
+
await staff.tasks.assign({ ...approval, assignee: { person: SUPERVISOR.personId } });
|
|
1362
1375
|
await staff.notify.send({
|
|
1363
1376
|
type: 'test.message',
|
|
1364
|
-
to: { person:
|
|
1377
|
+
to: { person: SUPERVISOR.personId },
|
|
1365
1378
|
subject: 'S',
|
|
1366
1379
|
body: 'B',
|
|
1367
1380
|
});
|
|
@@ -22,7 +22,7 @@ export type Recipient = {
|
|
|
22
22
|
export declare function parentsOf(studentId: string, options?: {
|
|
23
23
|
purpose?: ContactPurpose;
|
|
24
24
|
}): Recipient;
|
|
25
|
-
/**
|
|
25
|
+
/** The staff of a cohort: a class's teachers, or the staff who supervise a year group, roll group or house (D129). */
|
|
26
26
|
export declare function staffOf(cohortId: string): Recipient;
|
|
27
27
|
/** Student members of a cohort. */
|
|
28
28
|
export declare function studentsOf(cohortId: string): Recipient;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export function parentsOf(studentId, options = {}) {
|
|
3
3
|
return { relationship: 'parentsOf', studentId, purpose: options.purpose ?? 'correspondence' };
|
|
4
4
|
}
|
|
5
|
-
/**
|
|
5
|
+
/** The staff of a cohort: a class's teachers, or the staff who supervise a year group, roll group or house (D129). */
|
|
6
6
|
export function staffOf(cohortId) {
|
|
7
7
|
return { relationship: 'staffOf', cohortId };
|
|
8
8
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CanonicalRecord, ContactPurpose, Entities, EntityName, RecordWithTiers, Role, TiersFor } from '@quad-education/canonical';
|
|
2
2
|
import type { AppEntityDef, AppInput, AppSchema, FieldValue, SystemFields } from '@quad-education/schema';
|
|
3
3
|
import type { Recipient } from './recipients.ts';
|
|
4
|
-
export declare const SDK_VERSION = "0.
|
|
4
|
+
export declare const SDK_VERSION = "0.5.0";
|
|
5
5
|
/**
|
|
6
6
|
* Describes the current app to the type system. `quad gen` augments it from
|
|
7
7
|
* app.manifest.json and schema/ (D23); without that, nothing is readable.
|
|
@@ -103,7 +103,7 @@ export type SchoolInfo = {
|
|
|
103
103
|
readonly yearLevelLabels: Readonly<Record<string, string>>;
|
|
104
104
|
};
|
|
105
105
|
type CohortHelpers<A extends AppConfig> = {
|
|
106
|
-
/** The cohort with this
|
|
106
|
+
/** The cohort with this code, or null. Codes are the school's own and differ by school: never hard-code one (D129). */
|
|
107
107
|
byCode(code: string): Promise<SchoolRecord<A, 'cohorts'> | null>;
|
|
108
108
|
/** Memberships of a cohort. Needs a `cohortMemberships` scope. */
|
|
109
109
|
members(cohortId: string, options?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = '0.
|
|
1
|
+
export const SDK_VERSION = '0.5.0';
|
|
@@ -17,10 +17,19 @@ export type EngineOptions = {
|
|
|
17
17
|
export declare class Engine implements Kernel {
|
|
18
18
|
private readonly apps;
|
|
19
19
|
private clock;
|
|
20
|
-
private
|
|
21
|
-
|
|
20
|
+
private byId;
|
|
21
|
+
private school;
|
|
22
22
|
readonly store: AppStore;
|
|
23
23
|
private constructor();
|
|
24
|
+
/** The school the engine serves: the fictional school, or one a person made in the Sandbox (D130). */
|
|
25
|
+
get dataset(): Dataset;
|
|
26
|
+
/**
|
|
27
|
+
* Serve a different school from now on (D130): the Sandbox's School data view calls this after every change. Pass a
|
|
28
|
+
* dataset wrapped with `withDerived`; a new object, so every cache keyed by the dataset starts again. The apps' own
|
|
29
|
+
* data is untouched, and a reference to someone who has gone reads as not found.
|
|
30
|
+
*/
|
|
31
|
+
replaceSchool(dataset: Dataset): void;
|
|
32
|
+
private index;
|
|
24
33
|
static create(options: EngineOptions): Promise<Engine>;
|
|
25
34
|
close(): void;
|
|
26
35
|
/** Load an app, resetting its store if the schema changed since last time (D12). */
|
|
@@ -155,4 +164,4 @@ export declare class Engine implements Kernel {
|
|
|
155
164
|
/** D19: one CIO-visible entry per call that returns pastoral or medical fields. */
|
|
156
165
|
private auditSensitiveRead;
|
|
157
166
|
}
|
|
158
|
-
export declare const ENGINE_SDK_VERSION = "0.
|
|
167
|
+
export declare const ENGINE_SDK_VERSION = "0.5.0";
|
|
@@ -26,11 +26,28 @@ export class Engine {
|
|
|
26
26
|
apps = new Map();
|
|
27
27
|
clock = { kind: 'offset', offsetMs: 0 };
|
|
28
28
|
byId = new Map();
|
|
29
|
-
|
|
29
|
+
school;
|
|
30
30
|
store;
|
|
31
31
|
constructor(dataset, store) {
|
|
32
|
-
this.
|
|
32
|
+
this.school = dataset;
|
|
33
33
|
this.store = store;
|
|
34
|
+
this.index(dataset);
|
|
35
|
+
}
|
|
36
|
+
/** The school the engine serves: the fictional school, or one a person made in the Sandbox (D130). */
|
|
37
|
+
get dataset() {
|
|
38
|
+
return this.school;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Serve a different school from now on (D130): the Sandbox's School data view calls this after every change. Pass a
|
|
42
|
+
* dataset wrapped with `withDerived`; a new object, so every cache keyed by the dataset starts again. The apps' own
|
|
43
|
+
* data is untouched, and a reference to someone who has gone reads as not found.
|
|
44
|
+
*/
|
|
45
|
+
replaceSchool(dataset) {
|
|
46
|
+
this.index(dataset);
|
|
47
|
+
this.school = dataset;
|
|
48
|
+
}
|
|
49
|
+
index(dataset) {
|
|
50
|
+
const byId = new Map();
|
|
34
51
|
for (const entity of Object.keys(ENTITIES)) {
|
|
35
52
|
if (isDerived(entity)) {
|
|
36
53
|
// Worked out when an app first reads it (V2s S4.1), so it isn't indexed by id here.
|
|
@@ -40,9 +57,10 @@ export class Engine {
|
|
|
40
57
|
continue;
|
|
41
58
|
}
|
|
42
59
|
for (const record of dataset[entity]) {
|
|
43
|
-
|
|
60
|
+
byId.set(record.id, { entity, record });
|
|
44
61
|
}
|
|
45
62
|
}
|
|
63
|
+
this.byId = byId;
|
|
46
64
|
}
|
|
47
65
|
static async create(options) {
|
|
48
66
|
const store = options.store ??
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'fake-indexeddb/auto';
|
|
2
2
|
import type { Dataset, Principal } from '@quad-education/canonical';
|
|
3
|
-
import { type
|
|
3
|
+
import { type SuggestionRule } from '@quad-education/fictional-school';
|
|
4
4
|
import type { AppSchema } from '@quad-education/schema';
|
|
5
5
|
import type { AppConfig, CurrentApp, JsonValue, QuadSdk } from '../contract/types.ts';
|
|
6
6
|
import { Engine } from '../engine/engine.ts';
|
|
@@ -9,8 +9,11 @@ export type TestPlatform = {
|
|
|
9
9
|
readonly engine: Engine;
|
|
10
10
|
readonly appId: string;
|
|
11
11
|
readonly dataset: Dataset;
|
|
12
|
-
/**
|
|
13
|
-
|
|
12
|
+
/**
|
|
13
|
+
* An SDK for a suggested person by rule (e.g. 'parent-of-siblings'), anyone by id (e.g. 'stu-0042'), or any
|
|
14
|
+
* principal (D130).
|
|
15
|
+
*/
|
|
16
|
+
as(who: PersonWho | Principal): QuadSdk;
|
|
14
17
|
/** An SDK acting as the system principal, as jobs run. */
|
|
15
18
|
system(): QuadSdk;
|
|
16
19
|
setClock(iso: string | null): Promise<void>;
|
|
@@ -22,6 +25,8 @@ export declare function createTestPlatform(app: {
|
|
|
22
25
|
schema: AppSchema;
|
|
23
26
|
now?: string;
|
|
24
27
|
}): Promise<TestPlatform>;
|
|
28
|
+
/** A suggestion rule, such as `parent-of-siblings` (see `SUGGESTION_RULES`), or a person's id. */
|
|
29
|
+
export type PersonWho = SuggestionRule | (string & {});
|
|
25
30
|
/** What one run changed. Records count creates and updates by entity. */
|
|
26
31
|
export type Effects = {
|
|
27
32
|
outbox: number;
|
|
@@ -57,7 +62,7 @@ export declare function runJobTwice<A extends AppConfig = CurrentApp>(job: Job<A
|
|
|
57
62
|
* Who the run acts as (D7): the system principal (default, as for cron jobs), or the
|
|
58
63
|
* persona or principal who scheduled it.
|
|
59
64
|
*/
|
|
60
|
-
runAs?:
|
|
65
|
+
runAs?: PersonWho | Principal;
|
|
61
66
|
scheduledFor?: string;
|
|
62
67
|
payload?: JsonValue;
|
|
63
68
|
runId?: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @quad-education/sdk/testing: a test kit for app code. It runs the in-browser engine in memory
|
|
2
2
|
// (fake IndexedDB) with the fictional school, so app tests need no browser or shell.
|
|
3
3
|
import 'fake-indexeddb/auto';
|
|
4
|
-
import {
|
|
4
|
+
import { findPerson, withDerived } from '@quad-education/fictional-school';
|
|
5
5
|
import fictionalSchool from '@quad-education/fictional-school/dataset' with { type: 'json' };
|
|
6
6
|
import { connectInProcess } from "../engine/in-process.js";
|
|
7
7
|
import { Engine } from "../engine/engine.js";
|
|
@@ -15,34 +15,31 @@ export async function createTestPlatform(app) {
|
|
|
15
15
|
await engine.setClock(app.now);
|
|
16
16
|
await engine.installApp(app);
|
|
17
17
|
const appId = app.manifest.id;
|
|
18
|
-
const personas = personasFor(dataset);
|
|
19
18
|
return {
|
|
20
19
|
engine,
|
|
21
20
|
appId,
|
|
22
21
|
dataset,
|
|
23
22
|
as(who) {
|
|
24
|
-
|
|
25
|
-
return connectInProcess(engine, appId, who);
|
|
26
|
-
const p = personas.find((x) => x.key === who);
|
|
27
|
-
if (!p)
|
|
28
|
-
throw new Error(`unknown persona '${who}'`);
|
|
29
|
-
return connectInProcess(engine, appId, { kind: 'user', role: p.role, personId: p.personId });
|
|
23
|
+
return connectInProcess(engine, appId, toPrincipal(dataset, who));
|
|
30
24
|
},
|
|
31
25
|
system: () => connectInProcess(engine, appId, { kind: 'system', appId }),
|
|
32
26
|
setClock: (iso) => engine.setClock(iso),
|
|
33
27
|
close: () => engine.close(),
|
|
34
28
|
};
|
|
35
29
|
}
|
|
36
|
-
function
|
|
37
|
-
if (who === undefined)
|
|
38
|
-
return { kind: 'system', appId: platform.appId };
|
|
30
|
+
function toPrincipal(dataset, who) {
|
|
39
31
|
if (typeof who !== 'string')
|
|
40
32
|
return who;
|
|
41
|
-
const p =
|
|
33
|
+
const p = findPerson(dataset, who);
|
|
42
34
|
if (!p)
|
|
43
|
-
throw new Error(`
|
|
35
|
+
throw new Error(`no suggested person or person with id '${who}'`);
|
|
44
36
|
return { kind: 'user', role: p.role, personId: p.personId };
|
|
45
37
|
}
|
|
38
|
+
function resolvePrincipal(platform, who) {
|
|
39
|
+
if (who === undefined)
|
|
40
|
+
return { kind: 'system', appId: platform.appId };
|
|
41
|
+
return toPrincipal(platform.dataset, who);
|
|
42
|
+
}
|
|
46
43
|
function diff(before, after) {
|
|
47
44
|
const records = {};
|
|
48
45
|
for (const entity of new Set([...Object.keys(before.records), ...Object.keys(after.records)])) {
|
package/package.json
CHANGED
|
@@ -1,55 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quad-education/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The @quad-education/sdk contract, its in-browser engine, transports and contract tests",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
7
7
|
"exports": {
|
|
8
|
-
".":
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"./
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"./engine": {
|
|
17
|
-
"types": "./dist/src/engine/index.d.ts",
|
|
18
|
-
"default": "./dist/src/engine/index.js"
|
|
19
|
-
},
|
|
20
|
-
"./contract-tests": {
|
|
21
|
-
"types": "./dist/contract-tests/index.d.ts",
|
|
22
|
-
"default": "./dist/contract-tests/index.js"
|
|
23
|
-
},
|
|
24
|
-
"./jobs": {
|
|
25
|
-
"types": "./dist/src/jobs/index.d.ts",
|
|
26
|
-
"default": "./dist/src/jobs/index.js"
|
|
27
|
-
},
|
|
28
|
-
"./jobs-host": {
|
|
29
|
-
"types": "./dist/src/jobs/host.d.ts",
|
|
30
|
-
"default": "./dist/src/jobs/host.js"
|
|
31
|
-
},
|
|
32
|
-
"./jobs-worker": {
|
|
33
|
-
"types": "./dist/src/jobs/worker.d.ts",
|
|
34
|
-
"default": "./dist/src/jobs/worker.js"
|
|
35
|
-
},
|
|
36
|
-
"./testing": {
|
|
37
|
-
"types": "./dist/src/testing/index.d.ts",
|
|
38
|
-
"default": "./dist/src/testing/index.js"
|
|
39
|
-
}
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./client": "./src/client/connect.ts",
|
|
10
|
+
"./engine": "./src/engine/index.ts",
|
|
11
|
+
"./contract-tests": "./contract-tests/index.ts",
|
|
12
|
+
"./jobs": "./src/jobs/index.ts",
|
|
13
|
+
"./jobs-host": "./src/jobs/host.ts",
|
|
14
|
+
"./jobs-worker": "./src/jobs/worker.ts",
|
|
15
|
+
"./testing": "./src/testing/index.ts"
|
|
40
16
|
},
|
|
41
17
|
"files": [
|
|
42
18
|
"dist",
|
|
43
19
|
"LICENSE.md"
|
|
44
20
|
],
|
|
45
21
|
"publishConfig": {
|
|
46
|
-
"access": "public"
|
|
22
|
+
"access": "public",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/src/index.d.ts",
|
|
26
|
+
"default": "./dist/src/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./client": {
|
|
29
|
+
"types": "./dist/src/client/connect.d.ts",
|
|
30
|
+
"default": "./dist/src/client/connect.js"
|
|
31
|
+
},
|
|
32
|
+
"./engine": {
|
|
33
|
+
"types": "./dist/src/engine/index.d.ts",
|
|
34
|
+
"default": "./dist/src/engine/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./contract-tests": {
|
|
37
|
+
"types": "./dist/contract-tests/index.d.ts",
|
|
38
|
+
"default": "./dist/contract-tests/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./jobs": {
|
|
41
|
+
"types": "./dist/src/jobs/index.d.ts",
|
|
42
|
+
"default": "./dist/src/jobs/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./jobs-host": {
|
|
45
|
+
"types": "./dist/src/jobs/host.d.ts",
|
|
46
|
+
"default": "./dist/src/jobs/host.js"
|
|
47
|
+
},
|
|
48
|
+
"./jobs-worker": {
|
|
49
|
+
"types": "./dist/src/jobs/worker.d.ts",
|
|
50
|
+
"default": "./dist/src/jobs/worker.js"
|
|
51
|
+
},
|
|
52
|
+
"./testing": {
|
|
53
|
+
"types": "./dist/src/testing/index.d.ts",
|
|
54
|
+
"default": "./dist/src/testing/index.js"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp ../../LICENSE.md .",
|
|
60
|
+
"prepublishOnly": "corepack pnpm run build",
|
|
61
|
+
"typecheck": "tsc -p . && tsc -p tsconfig.core.json",
|
|
62
|
+
"api": "tsc -p tsconfig.api.json && api-extractor run --local",
|
|
63
|
+
"api:check": "tsc -p tsconfig.api.json && api-extractor run"
|
|
47
64
|
},
|
|
48
65
|
"dependencies": {
|
|
49
|
-
"
|
|
50
|
-
"@quad-education/
|
|
51
|
-
"@quad-education/
|
|
52
|
-
"
|
|
66
|
+
"@quad-education/canonical": "workspace:^",
|
|
67
|
+
"@quad-education/fictional-school": "workspace:^",
|
|
68
|
+
"@quad-education/schema": "workspace:^",
|
|
69
|
+
"fake-indexeddb": "^6.2.5"
|
|
53
70
|
},
|
|
54
71
|
"peerDependencies": {
|
|
55
72
|
"vitest": "^5.0.3"
|
|
@@ -64,11 +81,5 @@
|
|
|
64
81
|
"@types/node": "^24.19.1",
|
|
65
82
|
"typescript": "6.0.3",
|
|
66
83
|
"vitest": "^5.0.3"
|
|
67
|
-
},
|
|
68
|
-
"scripts": {
|
|
69
|
-
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp ../../LICENSE.md .",
|
|
70
|
-
"typecheck": "tsc -p . && tsc -p tsconfig.core.json",
|
|
71
|
-
"api": "tsc -p tsconfig.api.json && api-extractor run --local",
|
|
72
|
-
"api:check": "tsc -p tsconfig.api.json && api-extractor run"
|
|
73
84
|
}
|
|
74
|
-
}
|
|
85
|
+
}
|