@quad-education/sdk 0.0.0-stage → 0.1.3
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/LICENSE.md +15 -0
- package/README.md +106 -2
- package/dist/contract-tests/apps.d.ts +373 -0
- package/dist/contract-tests/apps.js +309 -0
- package/dist/contract-tests/index.d.ts +3 -0
- package/dist/contract-tests/index.js +2 -0
- package/dist/contract-tests/suite.d.ts +57 -0
- package/dist/contract-tests/suite.js +1405 -0
- package/dist/src/client/connect.d.ts +10 -0
- package/dist/src/client/connect.js +12 -0
- package/dist/src/client/create-client.d.ts +14 -0
- package/dist/src/client/create-client.js +76 -0
- package/dist/src/client/handshake-client.d.ts +16 -0
- package/dist/src/client/handshake-client.js +37 -0
- package/dist/src/contract/errors.d.ts +8 -0
- package/dist/src/contract/errors.js +13 -0
- package/dist/src/contract/recipients.d.ts +28 -0
- package/dist/src/contract/recipients.js +12 -0
- package/dist/src/contract/types.d.ts +442 -0
- package/dist/src/contract/types.js +1 -0
- package/dist/src/engine/audit.d.ts +27 -0
- package/dist/src/engine/audit.js +105 -0
- package/dist/src/engine/cron.d.ts +17 -0
- package/dist/src/engine/cron.js +143 -0
- package/dist/src/engine/db-indexes.d.ts +11 -0
- package/dist/src/engine/db-indexes.js +82 -0
- package/dist/src/engine/engine.d.ts +156 -0
- package/dist/src/engine/engine.js +953 -0
- package/dist/src/engine/host.d.ts +39 -0
- package/dist/src/engine/host.js +73 -0
- package/dist/src/engine/in-process.d.ts +5 -0
- package/dist/src/engine/in-process.js +18 -0
- package/dist/src/engine/index.d.ts +10 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/query.d.ts +16 -0
- package/dist/src/engine/query.js +106 -0
- package/dist/src/engine/runner.d.ts +26 -0
- package/dist/src/engine/runner.js +260 -0
- package/dist/src/engine/state.d.ts +107 -0
- package/dist/src/engine/state.js +16 -0
- package/dist/src/engine/storage.d.ts +46 -0
- package/dist/src/engine/storage.js +86 -0
- package/dist/src/engine/tasks.d.ts +17 -0
- package/dist/src/engine/tasks.js +245 -0
- package/dist/src/engine/validate-input.d.ts +9 -0
- package/dist/src/engine/validate-input.js +76 -0
- package/dist/src/engine/values.d.ts +10 -0
- package/dist/src/engine/values.js +58 -0
- package/dist/src/handshake.d.ts +16 -0
- package/dist/src/handshake.js +9 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +4 -0
- package/dist/src/jobs/define.d.ts +41 -0
- package/dist/src/jobs/define.js +28 -0
- package/dist/src/jobs/host.d.ts +13 -0
- package/dist/src/jobs/host.js +22 -0
- package/dist/src/jobs/index.d.ts +1 -0
- package/dist/src/jobs/index.js +2 -0
- package/dist/src/jobs/worker.d.ts +17 -0
- package/dist/src/jobs/worker.js +25 -0
- package/dist/src/platform-types.d.ts +19 -0
- package/dist/src/platform-types.js +1 -0
- package/dist/src/port-rpc.d.ts +42 -0
- package/dist/src/port-rpc.js +85 -0
- package/dist/src/protocol.d.ts +41 -0
- package/dist/src/protocol.js +45 -0
- package/dist/src/testing/index.d.ts +78 -0
- package/dist/src/testing/index.js +118 -0
- package/package.json +71 -3
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { boolean, defineSchema, entity, enumOf, number, ref, text, } from '@quad-education/schema';
|
|
2
|
+
import { staffOf } from "../src/contract/recipients.js";
|
|
3
|
+
import { defineJob } from "../src/jobs/index.js";
|
|
4
|
+
/** The schema the contract suite's test apps use. */
|
|
5
|
+
export const TEST_SCHEMA = defineSchema({
|
|
6
|
+
greeting: entity({
|
|
7
|
+
fields: {
|
|
8
|
+
message: text(),
|
|
9
|
+
studentId: ref('school.students'),
|
|
10
|
+
tone: enumOf(['warm', 'formal']),
|
|
11
|
+
note: text({ optional: true }),
|
|
12
|
+
},
|
|
13
|
+
// D37: sorts and range filters need an index. createdAt and id are always indexed.
|
|
14
|
+
indexes: [['studentId'], ['message'], ['tone', 'message']],
|
|
15
|
+
subject: 'studentId',
|
|
16
|
+
access: {
|
|
17
|
+
staff: { read: 'all', create: 'all', update: 'all', delete: 'all' },
|
|
18
|
+
parent: { read: 'subject' },
|
|
19
|
+
student: { read: 'subject' },
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
reply: entity({
|
|
23
|
+
fields: { greetingId: ref('greeting'), message: text() },
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
const base = {
|
|
27
|
+
manifestVersion: 0,
|
|
28
|
+
sdk: '^0.1.0',
|
|
29
|
+
kit: 'react-ui',
|
|
30
|
+
roles: ['staff', 'student', 'parent'],
|
|
31
|
+
tasks: [],
|
|
32
|
+
scheduled: [],
|
|
33
|
+
cron: [],
|
|
34
|
+
};
|
|
35
|
+
/** A test app with a broad set of scopes. */
|
|
36
|
+
export const TEST_APP = {
|
|
37
|
+
manifest: {
|
|
38
|
+
...base,
|
|
39
|
+
roles: [...base.roles],
|
|
40
|
+
tasks: [],
|
|
41
|
+
scheduled: [],
|
|
42
|
+
cron: [],
|
|
43
|
+
id: 'contract-test',
|
|
44
|
+
name: 'Contract test app',
|
|
45
|
+
scopes: [
|
|
46
|
+
'students.basic:read',
|
|
47
|
+
'students.contact:read',
|
|
48
|
+
'staff.basic:read',
|
|
49
|
+
'staff.contact:read',
|
|
50
|
+
'parents.basic:read',
|
|
51
|
+
'parents.contact:read',
|
|
52
|
+
'parentStudentLinks.basic:read',
|
|
53
|
+
'cohorts.basic:read',
|
|
54
|
+
'cohortMemberships.basic:read',
|
|
55
|
+
'medicalAlerts.medical:read',
|
|
56
|
+
'pastoralNotes.pastoral:read',
|
|
57
|
+
'calendarEvents.basic:read',
|
|
58
|
+
'courses.basic:read',
|
|
59
|
+
'results.academic:read',
|
|
60
|
+
'lessons.basic:read',
|
|
61
|
+
'periods.basic:read',
|
|
62
|
+
'rooms.basic:read',
|
|
63
|
+
'subjects.basic:read',
|
|
64
|
+
],
|
|
65
|
+
notifications: ['test.message', 'test.reminder'],
|
|
66
|
+
},
|
|
67
|
+
schema: TEST_SCHEMA,
|
|
68
|
+
};
|
|
69
|
+
/** A test app with only `students.basic:read`. */
|
|
70
|
+
export const BASIC_APP = {
|
|
71
|
+
manifest: {
|
|
72
|
+
...TEST_APP.manifest,
|
|
73
|
+
id: 'contract-basic',
|
|
74
|
+
name: 'Basic scopes app',
|
|
75
|
+
scopes: ['students.basic:read'],
|
|
76
|
+
notifications: ['test.message'],
|
|
77
|
+
},
|
|
78
|
+
schema: TEST_SCHEMA,
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* A version 1 manifest (V2s D85): students are read with `dateOfBirth` required and `email` optional,
|
|
82
|
+
* and nothing else that varies. The core fields (studentNumber, givenName, familyName) come with the
|
|
83
|
+
* entity. Its scopes are exactly the tiers those fields need.
|
|
84
|
+
*/
|
|
85
|
+
export const DATA_APP = {
|
|
86
|
+
manifest: {
|
|
87
|
+
...TEST_APP.manifest,
|
|
88
|
+
manifestVersion: 1,
|
|
89
|
+
id: 'contract-data',
|
|
90
|
+
name: 'Declared fields app',
|
|
91
|
+
scopes: ['students.basic:read', 'students.contact:read'],
|
|
92
|
+
data: { students: { required: ['dateOfBirth'], optional: ['email'] } },
|
|
93
|
+
reasons: { 'students.contact:read': 'To show each student’s birthday in the contract tests.' },
|
|
94
|
+
notifications: ['test.message'],
|
|
95
|
+
},
|
|
96
|
+
schema: TEST_SCHEMA,
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* A standalone app (V2s D84): version 1, two app-defined roles named like school roles, so the matching
|
|
100
|
+
* personas can preview them. The access keys are written in different cases on purpose.
|
|
101
|
+
*/
|
|
102
|
+
export const ROLES_SCHEMA = defineSchema({
|
|
103
|
+
entry: entity({
|
|
104
|
+
fields: { body: text() },
|
|
105
|
+
access: {
|
|
106
|
+
Staff: { read: 'all', create: 'own', update: 'own', delete: 'own' },
|
|
107
|
+
student: { read: 'own', create: 'own' },
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
export const ROLES_APP = {
|
|
112
|
+
manifest: {
|
|
113
|
+
...TEST_APP.manifest,
|
|
114
|
+
manifestVersion: 1,
|
|
115
|
+
id: 'contract-roles',
|
|
116
|
+
name: 'App roles app',
|
|
117
|
+
roles: ['Staff', 'Student'],
|
|
118
|
+
schoolRoles: [],
|
|
119
|
+
scopes: [],
|
|
120
|
+
notifications: [],
|
|
121
|
+
},
|
|
122
|
+
schema: ROLES_SCHEMA,
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* D25 access control. The documented pattern: a shared record (`excursion`) is staff-only,
|
|
126
|
+
* and parents work with per-student rows (`consent`) whose subject is their child.
|
|
127
|
+
*/
|
|
128
|
+
export const ACCESS_SCHEMA = defineSchema({
|
|
129
|
+
excursion: entity({ fields: { title: text() } }),
|
|
130
|
+
consent: entity({
|
|
131
|
+
fields: {
|
|
132
|
+
studentId: ref('school.students'),
|
|
133
|
+
excursionId: ref('excursion'),
|
|
134
|
+
status: enumOf(['pending', 'given', 'declined']),
|
|
135
|
+
staffNote: text({ optional: true, visibleTo: ['staff'] }),
|
|
136
|
+
},
|
|
137
|
+
subject: 'studentId',
|
|
138
|
+
access: {
|
|
139
|
+
staff: { read: 'all', create: 'all', update: 'all', delete: 'all' },
|
|
140
|
+
parent: { read: 'subject', update: 'subject' },
|
|
141
|
+
student: { read: 'subject' },
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
message: entity({
|
|
145
|
+
fields: {
|
|
146
|
+
studentId: ref('school.students'),
|
|
147
|
+
consentId: ref('consent', { optional: true }),
|
|
148
|
+
body: text(),
|
|
149
|
+
},
|
|
150
|
+
indexes: [['body']],
|
|
151
|
+
subject: 'studentId',
|
|
152
|
+
access: {
|
|
153
|
+
staff: { read: 'all', create: 'all', delete: 'all' },
|
|
154
|
+
parent: { read: 'subject', create: 'subject', update: 'own', delete: 'own' },
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
export const ACCESS_APP = {
|
|
159
|
+
manifest: { ...TEST_APP.manifest, id: 'contract-access', name: 'Access control app' },
|
|
160
|
+
schema: ACCESS_SCHEMA,
|
|
161
|
+
};
|
|
162
|
+
// ---- Slice B: tasks, jobs, audit ----
|
|
163
|
+
/** A staff-only log that test jobs write to, so tests can see what ran and as whom. */
|
|
164
|
+
export const JOBS_SCHEMA = defineSchema({
|
|
165
|
+
...TEST_SCHEMA,
|
|
166
|
+
/** What a job could see and do, recorded by the job itself (D7 run-as tests). */
|
|
167
|
+
probe: entity({
|
|
168
|
+
fields: {
|
|
169
|
+
runId: text(),
|
|
170
|
+
principal: text(),
|
|
171
|
+
studentsSeen: number(),
|
|
172
|
+
otherFamilyVisible: boolean(),
|
|
173
|
+
staffOnlyWriteAllowed: boolean(),
|
|
174
|
+
},
|
|
175
|
+
access: {
|
|
176
|
+
staff: { read: 'all', create: 'all' },
|
|
177
|
+
parent: { read: 'own', create: 'own' },
|
|
178
|
+
student: { read: 'own', create: 'own' },
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
jobLog: entity({
|
|
182
|
+
fields: {
|
|
183
|
+
jobName: text(),
|
|
184
|
+
runId: text(),
|
|
185
|
+
scheduledFor: text(),
|
|
186
|
+
principal: text(),
|
|
187
|
+
note: text({ optional: true }),
|
|
188
|
+
},
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
/** Lets a test hold the `slow` job mid-run, to prove runs of one job never overlap. */
|
|
192
|
+
export const slowGate = {
|
|
193
|
+
release: () => { },
|
|
194
|
+
wait: Promise.resolve(),
|
|
195
|
+
reset() {
|
|
196
|
+
this.wait = new Promise((resolve) => (this.release = resolve));
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
const flakyCalls = new Map();
|
|
200
|
+
/** The idempotent pattern every job should follow (D20): derive keys from runId. */
|
|
201
|
+
async function logRun(ctx, note) {
|
|
202
|
+
const me = await ctx.sdk.auth.me();
|
|
203
|
+
await ctx.sdk.db.entity('jobLog').create({
|
|
204
|
+
jobName: ctx.jobName,
|
|
205
|
+
runId: ctx.runId,
|
|
206
|
+
scheduledFor: ctx.scheduledFor,
|
|
207
|
+
principal: me.kind,
|
|
208
|
+
...(note !== undefined ? { note } : {}),
|
|
209
|
+
}, { idempotencyKey: `log:${ctx.runId}` });
|
|
210
|
+
await ctx.sdk.audit.record({
|
|
211
|
+
action: 'job.logged',
|
|
212
|
+
summary: `${ctx.jobName} ran`,
|
|
213
|
+
idempotencyKey: `audit:${ctx.runId}`,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/** Records who the job ran as and what it could reach (D7). */
|
|
217
|
+
const probe = defineJob({
|
|
218
|
+
name: 'probe',
|
|
219
|
+
async run({ sdk, runId, payload }) {
|
|
220
|
+
const me = await sdk.auth.me();
|
|
221
|
+
const students = await sdk.school.students.list({ limit: 200 });
|
|
222
|
+
const otherId = payload?.otherGreetingId;
|
|
223
|
+
const otherFamilyVisible = otherId
|
|
224
|
+
? await sdk.db
|
|
225
|
+
.entity('greeting')
|
|
226
|
+
.get(otherId)
|
|
227
|
+
.then(() => true, () => false)
|
|
228
|
+
: false;
|
|
229
|
+
const staffOnlyWriteAllowed = await sdk.db
|
|
230
|
+
.entity('jobLog')
|
|
231
|
+
.create({ jobName: 'probe', runId, scheduledFor: 'n/a', principal: me.kind }, { idempotencyKey: `probe-log:${runId}` })
|
|
232
|
+
.then(() => true, () => false);
|
|
233
|
+
await sdk.db.entity('probe').create({
|
|
234
|
+
runId,
|
|
235
|
+
principal: me.kind === 'user' ? `${me.role}:${me.person.id}` : 'system',
|
|
236
|
+
studentsSeen: students.items.length,
|
|
237
|
+
otherFamilyVisible,
|
|
238
|
+
staffOnlyWriteAllowed,
|
|
239
|
+
}, { idempotencyKey: `probe:${runId}` });
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
/** Schedules `probe` from inside a job: it runs as this job's principal, never above it. */
|
|
243
|
+
const chain = defineJob({
|
|
244
|
+
name: 'chain',
|
|
245
|
+
async run({ sdk, runId, scheduledFor, payload }) {
|
|
246
|
+
await sdk.schedule.at({
|
|
247
|
+
job: 'probe',
|
|
248
|
+
when: scheduledFor,
|
|
249
|
+
payload,
|
|
250
|
+
idempotencyKey: `chain:${runId}`,
|
|
251
|
+
});
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
export const TEST_JOBS = [
|
|
255
|
+
probe,
|
|
256
|
+
chain,
|
|
257
|
+
defineJob({
|
|
258
|
+
name: 'weekly',
|
|
259
|
+
async run(ctx) {
|
|
260
|
+
await logRun(ctx);
|
|
261
|
+
const team = await ctx.sdk.school.cohorts.byCode('deputy-principal');
|
|
262
|
+
await ctx.sdk.notify.send({
|
|
263
|
+
type: 'test.message',
|
|
264
|
+
to: staffOf(team.id),
|
|
265
|
+
subject: 'Weekly summary',
|
|
266
|
+
body: 'Summary',
|
|
267
|
+
idempotencyKey: `notify:${ctx.runId}`,
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
}),
|
|
271
|
+
defineJob({
|
|
272
|
+
name: 'follow-up',
|
|
273
|
+
async run(ctx) {
|
|
274
|
+
await logRun(ctx, JSON.stringify(ctx.payload));
|
|
275
|
+
},
|
|
276
|
+
}),
|
|
277
|
+
defineJob({
|
|
278
|
+
name: 'slow',
|
|
279
|
+
async run(ctx) {
|
|
280
|
+
await slowGate.wait;
|
|
281
|
+
await logRun(ctx);
|
|
282
|
+
},
|
|
283
|
+
}),
|
|
284
|
+
defineJob({
|
|
285
|
+
name: 'flaky',
|
|
286
|
+
async run(ctx) {
|
|
287
|
+
const calls = (flakyCalls.get(ctx.runId) ?? 0) + 1;
|
|
288
|
+
flakyCalls.set(ctx.runId, calls);
|
|
289
|
+
if (calls <= 2)
|
|
290
|
+
throw new Error(`flaky failure ${calls}`);
|
|
291
|
+
await logRun(ctx);
|
|
292
|
+
},
|
|
293
|
+
}),
|
|
294
|
+
];
|
|
295
|
+
export const JOBS_APP = {
|
|
296
|
+
manifest: {
|
|
297
|
+
...TEST_APP.manifest,
|
|
298
|
+
id: 'contract-jobs',
|
|
299
|
+
name: 'Tasks and jobs app',
|
|
300
|
+
notifications: ['test.message'],
|
|
301
|
+
tasks: ['test.approval', 'test.consent'],
|
|
302
|
+
scheduled: ['follow-up', 'slow', 'flaky', 'probe', 'chain'],
|
|
303
|
+
cron: [
|
|
304
|
+
{ name: 'weekly', schedule: '0 7 * * 1', description: 'Mondays at 07:00, school time' },
|
|
305
|
+
],
|
|
306
|
+
},
|
|
307
|
+
schema: JOBS_SCHEMA,
|
|
308
|
+
jobs: TEST_JOBS,
|
|
309
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type Principal } from '@quad-education/canonical';
|
|
2
|
+
import type { AppSchema, Manifest } from '@quad-education/schema';
|
|
3
|
+
import type { OutboxEntry } from '../src/platform-types.ts';
|
|
4
|
+
import type { Job } from '../src/jobs/index.ts';
|
|
5
|
+
export type TestApp = {
|
|
6
|
+
manifest: Manifest;
|
|
7
|
+
schema: AppSchema;
|
|
8
|
+
jobs?: readonly Job<any>[];
|
|
9
|
+
};
|
|
10
|
+
/** A run as the harness reports it (the engine's view of a job delivery). */
|
|
11
|
+
export type RunSummary = {
|
|
12
|
+
id: string;
|
|
13
|
+
jobName: string;
|
|
14
|
+
scheduledFor: string;
|
|
15
|
+
status: 'pending' | 'running' | 'succeeded' | 'failed';
|
|
16
|
+
attempts: number;
|
|
17
|
+
deliveries: number;
|
|
18
|
+
};
|
|
19
|
+
/** One running implementation of the SDK contract, loaded with the fictional school. */
|
|
20
|
+
export type ContractEnv = {
|
|
21
|
+
/** An SDK connected as `principal`. Every connection shares the same engine and storage. */
|
|
22
|
+
connect(principal: Principal): Promise<any>;
|
|
23
|
+
control: {
|
|
24
|
+
/** Freeze the engine clock at an instant, or `null` for real time. */
|
|
25
|
+
setClock(iso: string | null): Promise<void>;
|
|
26
|
+
/** Clear the app's data and outbox (the shell's reset button). */
|
|
27
|
+
reset(): Promise<void>;
|
|
28
|
+
outbox(): Promise<readonly OutboxEntry[]>;
|
|
29
|
+
/** Start a new engine over the same storage, as after a page reload; optionally with a changed app. */
|
|
30
|
+
restart(app?: Partial<TestApp>): Promise<void>;
|
|
31
|
+
/** Move the clock forward (D14). */
|
|
32
|
+
advanceClock(ms: number): Promise<void>;
|
|
33
|
+
/** Start due runs, execute them with the app's jobs as the system principal, and report back. */
|
|
34
|
+
runDueJobs(): Promise<{
|
|
35
|
+
runId: string;
|
|
36
|
+
jobName: string;
|
|
37
|
+
scheduledFor: string;
|
|
38
|
+
ok: boolean;
|
|
39
|
+
}[]>;
|
|
40
|
+
/** Deliver a finished run again, with the same runId (D20). */
|
|
41
|
+
redeliver(runId: string): Promise<void>;
|
|
42
|
+
runs(): Promise<readonly RunSummary[]>;
|
|
43
|
+
/** Open tasks assigned to a person: the shell's inbox (D6). */
|
|
44
|
+
inbox(personId: string): Promise<readonly {
|
|
45
|
+
id: string;
|
|
46
|
+
type: string;
|
|
47
|
+
title: string;
|
|
48
|
+
}[]>;
|
|
49
|
+
/** Every audit entry, including CIO-only ones (D19). */
|
|
50
|
+
auditLog(): Promise<readonly (Record<string, any> & {
|
|
51
|
+
visibility: 'app' | 'cio';
|
|
52
|
+
})[]>;
|
|
53
|
+
};
|
|
54
|
+
dispose(): Promise<void>;
|
|
55
|
+
};
|
|
56
|
+
export type ContractFactory = (app: TestApp) => Promise<ContractEnv>;
|
|
57
|
+
export declare function defineContractSuite(name: string, factory: ContractFactory): void;
|