@quad-education/sdk 0.1.4 → 0.2.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/README.md +1 -0
- package/dist/contract-tests/apps.d.ts +79 -0
- package/dist/contract-tests/apps.js +56 -0
- package/dist/contract-tests/harness.d.ts +11 -0
- package/dist/contract-tests/harness.js +40 -0
- package/dist/contract-tests/helpers.d.ts +5 -0
- package/dist/contract-tests/helpers.js +25 -0
- package/dist/contract-tests/index.d.ts +4 -2
- package/dist/contract-tests/index.js +3 -1
- package/dist/contract-tests/standalone-apps.d.ts +183 -0
- package/dist/contract-tests/standalone-apps.js +178 -0
- package/dist/contract-tests/standalone.d.ts +2 -0
- package/dist/contract-tests/standalone.js +709 -0
- package/dist/contract-tests/suite.d.ts +10 -3
- package/dist/contract-tests/suite.js +207 -42
- package/dist/src/client/connect.d.ts +13 -3
- package/dist/src/client/connect.js +21 -3
- package/dist/src/client/handshake-client.d.ts +2 -0
- package/dist/src/client/handshake-client.js +2 -0
- package/dist/src/client/http-transport.d.ts +18 -0
- package/dist/src/client/http-transport.js +76 -0
- package/dist/src/client/live.d.ts +39 -0
- package/dist/src/client/live.js +122 -0
- package/dist/src/contract/errors.d.ts +1 -1
- package/dist/src/contract/principals.d.ts +30 -0
- package/dist/src/contract/principals.js +12 -0
- package/dist/src/contract/recipients.d.ts +8 -2
- package/dist/src/contract/types.d.ts +18 -2
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/audit.d.ts +2 -2
- package/dist/src/engine/audit.js +8 -6
- package/dist/src/engine/db-indexes.js +6 -1
- package/dist/src/engine/engine.d.ts +53 -17
- package/dist/src/engine/engine.js +260 -156
- package/dist/src/engine/host.d.ts +2 -2
- package/dist/src/engine/host.js +1 -1
- package/dist/src/engine/in-process.d.ts +2 -2
- package/dist/src/engine/index.d.ts +3 -1
- package/dist/src/engine/index.js +1 -0
- package/dist/src/engine/memory-store.d.ts +32 -0
- package/dist/src/engine/memory-store.js +123 -0
- package/dist/src/engine/runner.d.ts +2 -2
- package/dist/src/engine/runner.js +31 -26
- package/dist/src/engine/state.d.ts +21 -15
- package/dist/src/engine/state.js +31 -3
- package/dist/src/engine/store.d.ts +48 -0
- package/dist/src/engine/store.js +1 -0
- package/dist/src/engine/tasks.d.ts +4 -6
- package/dist/src/engine/tasks.js +27 -24
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/jobs/define.d.ts +2 -2
- package/dist/src/platform-types.d.ts +2 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -95,6 +95,7 @@ The types allow `orderBy` and ranges only on indexed fields, so most mistakes ar
|
|
|
95
95
|
|
|
96
96
|
## Versions
|
|
97
97
|
|
|
98
|
+
- **0.2.0 (V2c).** For standalone apps that go live, where there is no school. Two new principals beside the school's users: a Quad **account**, holding the app's own roles, and a **guest**, who isn't signed in and is known by a device id. `auth.me()` returns `{ kind: 'account', account, roles }` and `{ kind: 'guest', guest }`. A new error code, `NOT_AVAILABLE`, is returned for everything under `sdk.school`, for `{ person }`, `parentsOf`, `staffOf` and `studentsOf` recipients and for `regarding`, whenever the caller is an account or guest, the app is standalone, or the environment has no school. Access rules match all of an account's roles, ignoring case, and the reserved role `guest` may only be given `own`. `createdBy, createdAt` is an implicit index, so a list of one's own records can be sorted. Notifications and tasks accept `{ account: id }` and `{ role: '<app role>' }`; guests can't send, assign or schedule. A version 1 manifest gains `guests?: boolean` (standalone apps only, and it needs 0.2.0). Nothing changes for school apps. The in-browser engine (`@quad-education/sdk/engine`, platform-side only) now keeps its state behind an `AppStore`, so the serverless data API can run the same rules over DynamoDB; as a result its `inbox`, `runs`, `auditLog`, `effectsSnapshot` and `runPrincipal` return promises, and an engine can be created with `school: false`. **A live app connects the same way**: `connect()` from `@quad-education/sdk/client` (and `<QuadProvider>` from `@quad-education/sdk-react`) works in the shell's iframe and, with no shell, at a live address, where it asks its own origin who is signed in and sends calls to the data API with a short-lived token. With nobody signed in it goes to Quad's sign-in, or, if the app allows guests, throws `SignInRequired` (`QuadProvider` shows sign-in and guest choices; `signIn()` and `continueAsGuest()` are exported). A live app needs 0.2.0.
|
|
98
99
|
- **0.1.4** (19 Oct 2026). The same SDK calls. The canonical model grows to what V2s S4 builds, which is what an app's `sdk.school.<entity>` and its types come from, so **0.1.3 cannot use any of it**: the attendance register (`sessionAttendance`, which must be filtered by student or date; `studentAbsences`, `attendanceSummaries`), behaviour and achievements, learning support, medical events and notes, staff absences and admissions applicants, and the demographic fields on `students`. It also adds two sensitivity tiers, `sensitive` and `hr`, which are staff-only (a student or parent never receives them), audited, and need manifest version 1 and a reason. A version 1 manifest still needs 0.1.3 or newer.
|
|
99
100
|
- **0.1.3** (18 Oct 2026). The same SDK calls, plus what V2s needs to run: manifest version 1 (`data`, `reasons`, `roles` and `schoolRoles`), declared-field types and stripping for school data, the new school entities (`periods`, `rooms`, `subjects`, `lessons`, with `lessons` generated on demand) and the test toolkit that understands all of it. **A version 1 manifest needs 0.1.3 or newer**: the test toolkit in 0.1.2 only reads version 0 manifests and fails every test with `invalid manifest: manifestVersion: expected 0`. The starter templates ask for `^0.1.3`.
|
|
100
101
|
- **0.1.2** (9 Oct 2026). The same contract as 0.1.1, and the first version to stay on npm. All the `@quad-education/*` packages were published on 9 Oct 2026 (0.1.0, and 0.1.1 for this one), then unpublished the same day to move the npm org to a new owner, and npm never allows an unpublished version again; every package is now 0.1.2. `SDK_VERSION` (`sdk.version`) now matches the package version; it had stayed at 0.1.0 through 0.1.1.
|
|
@@ -93,6 +93,85 @@ export declare const WELLBEING_APP: {
|
|
|
93
93
|
manifest: Manifest;
|
|
94
94
|
schema: typeof TEST_SCHEMA;
|
|
95
95
|
};
|
|
96
|
+
/** The account directory the contract suite's engines are given: who exists and which app roles they hold. */
|
|
97
|
+
export declare const TEST_ACCOUNTS: readonly [{
|
|
98
|
+
readonly id: "acc-ada";
|
|
99
|
+
readonly displayName: "Ada Teacher";
|
|
100
|
+
readonly roles: readonly ["Teacher"];
|
|
101
|
+
}, {
|
|
102
|
+
readonly id: "acc-ben";
|
|
103
|
+
readonly displayName: "Ben Both";
|
|
104
|
+
readonly roles: readonly ["Teacher", "Student"];
|
|
105
|
+
}, {
|
|
106
|
+
readonly id: "acc-cai";
|
|
107
|
+
readonly displayName: "Cai Student";
|
|
108
|
+
readonly roles: readonly ["Student"];
|
|
109
|
+
}, {
|
|
110
|
+
readonly id: "acc-dee";
|
|
111
|
+
readonly displayName: "Dee Student";
|
|
112
|
+
readonly roles: readonly ["Student"];
|
|
113
|
+
}, {
|
|
114
|
+
readonly id: "acc-eve";
|
|
115
|
+
readonly displayName: "Eve Staff";
|
|
116
|
+
readonly roles: readonly ["Staff"];
|
|
117
|
+
}, {
|
|
118
|
+
readonly id: "acc-gus";
|
|
119
|
+
readonly displayName: "Gus Staff";
|
|
120
|
+
readonly roles: readonly ["Staff"];
|
|
121
|
+
}];
|
|
122
|
+
declare const ACCOUNT_SCHEMA: {
|
|
123
|
+
readonly note: {
|
|
124
|
+
readonly fields: {
|
|
125
|
+
readonly body: {
|
|
126
|
+
readonly kind: "text";
|
|
127
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
128
|
+
};
|
|
129
|
+
readonly score: {
|
|
130
|
+
readonly kind: "number";
|
|
131
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
132
|
+
} & {
|
|
133
|
+
readonly optional: true;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
readonly indexes: readonly [readonly ["score"]];
|
|
137
|
+
readonly subject?: "score" | "body";
|
|
138
|
+
readonly access?: {
|
|
139
|
+
readonly teacher: {
|
|
140
|
+
readonly read: "all";
|
|
141
|
+
readonly create: "all";
|
|
142
|
+
readonly update: "all";
|
|
143
|
+
readonly delete: "all";
|
|
144
|
+
};
|
|
145
|
+
readonly Student: {
|
|
146
|
+
readonly read: "own";
|
|
147
|
+
readonly create: "own";
|
|
148
|
+
readonly update: "own";
|
|
149
|
+
readonly delete: "own";
|
|
150
|
+
};
|
|
151
|
+
readonly guest: {
|
|
152
|
+
readonly read: "own";
|
|
153
|
+
readonly create: "own";
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
type AccountApp = {
|
|
159
|
+
roles: 'teacher' | 'student';
|
|
160
|
+
scopes: never;
|
|
161
|
+
notifications: 'test.message';
|
|
162
|
+
tasks: 'test.review';
|
|
163
|
+
scheduled: 'remind';
|
|
164
|
+
schema: typeof ACCOUNT_SCHEMA;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* A standalone app (version 1) with two app roles and guests (V2c). It reads no school data, so everything under
|
|
168
|
+
* `sdk.school` is NOT_AVAILABLE for it, whoever is asking.
|
|
169
|
+
*/
|
|
170
|
+
export declare const ACCOUNT_APP: {
|
|
171
|
+
manifest: Manifest;
|
|
172
|
+
schema: typeof ACCOUNT_SCHEMA;
|
|
173
|
+
jobs: Job<AccountApp>[];
|
|
174
|
+
};
|
|
96
175
|
/** A test app with only `students.basic:read`. */
|
|
97
176
|
export declare const BASIC_APP: {
|
|
98
177
|
manifest: Manifest;
|
|
@@ -177,6 +177,62 @@ export const WELLBEING_APP = {
|
|
|
177
177
|
},
|
|
178
178
|
schema: TEST_SCHEMA,
|
|
179
179
|
};
|
|
180
|
+
// ---- Accounts and guests (V2c, D100) ----
|
|
181
|
+
/** The account directory the contract suite's engines are given: who exists and which app roles they hold. */
|
|
182
|
+
export const TEST_ACCOUNTS = [
|
|
183
|
+
{ id: 'acc-ada', displayName: 'Ada Teacher', roles: ['Teacher'] },
|
|
184
|
+
{ id: 'acc-ben', displayName: 'Ben Both', roles: ['Teacher', 'Student'] },
|
|
185
|
+
{ id: 'acc-cai', displayName: 'Cai Student', roles: ['Student'] },
|
|
186
|
+
{ id: 'acc-dee', displayName: 'Dee Student', roles: ['Student'] },
|
|
187
|
+
{ id: 'acc-eve', displayName: 'Eve Staff', roles: ['Staff'] },
|
|
188
|
+
{ id: 'acc-gus', displayName: 'Gus Staff', roles: ['Staff'] },
|
|
189
|
+
];
|
|
190
|
+
const ACCOUNT_SCHEMA = defineSchema({
|
|
191
|
+
note: entity({
|
|
192
|
+
fields: { body: text(), score: number({ optional: true }) },
|
|
193
|
+
indexes: [['score']],
|
|
194
|
+
// The keys are written in different cases on purpose: they match the app's roles ignoring case.
|
|
195
|
+
access: {
|
|
196
|
+
teacher: { read: 'all', create: 'all', update: 'all', delete: 'all' },
|
|
197
|
+
Student: { read: 'own', create: 'own', update: 'own', delete: 'own' },
|
|
198
|
+
guest: { read: 'own', create: 'own' },
|
|
199
|
+
},
|
|
200
|
+
}),
|
|
201
|
+
});
|
|
202
|
+
/** Records who it ran as, so a test can see the run-as principal of a job an account scheduled. */
|
|
203
|
+
const remind = defineJob({
|
|
204
|
+
name: 'remind',
|
|
205
|
+
async run({ sdk, runId }) {
|
|
206
|
+
const me = await sdk.auth.me();
|
|
207
|
+
await sdk.db
|
|
208
|
+
.entity('note')
|
|
209
|
+
.create({ body: JSON.stringify(me) }, { idempotencyKey: `remind:${runId}` });
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
/**
|
|
213
|
+
* A standalone app (version 1) with two app roles and guests (V2c). It reads no school data, so everything under
|
|
214
|
+
* `sdk.school` is NOT_AVAILABLE for it, whoever is asking.
|
|
215
|
+
*/
|
|
216
|
+
export const ACCOUNT_APP = {
|
|
217
|
+
manifest: {
|
|
218
|
+
manifestVersion: 1,
|
|
219
|
+
id: 'contract-accounts',
|
|
220
|
+
name: 'Accounts app',
|
|
221
|
+
sdk: '^0.2.0',
|
|
222
|
+
kit: 'react-ui',
|
|
223
|
+
roles: ['Teacher', 'Student'],
|
|
224
|
+
schoolRoles: [],
|
|
225
|
+
guests: true,
|
|
226
|
+
scopes: [],
|
|
227
|
+
data: {},
|
|
228
|
+
notifications: ['test.message'],
|
|
229
|
+
tasks: ['test.review'],
|
|
230
|
+
scheduled: ['remind'],
|
|
231
|
+
cron: [],
|
|
232
|
+
},
|
|
233
|
+
schema: ACCOUNT_SCHEMA,
|
|
234
|
+
jobs: [remind],
|
|
235
|
+
};
|
|
180
236
|
/** A test app with only `students.basic:read`. */
|
|
181
237
|
export const BASIC_APP = {
|
|
182
238
|
manifest: {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ActingAs } from '../src/contract/principals.ts';
|
|
2
|
+
import type { ContractEnv, TestApp } from './suite.ts';
|
|
3
|
+
import type { QuadSdk } from '../src/contract/types.ts';
|
|
4
|
+
import type { Engine } from '../src/engine/index.ts';
|
|
5
|
+
type SliceBControl = Pick<ContractEnv['control'], 'advanceClock' | 'runDueJobs' | 'redeliver' | 'runs' | 'inbox' | 'auditLog'>;
|
|
6
|
+
/**
|
|
7
|
+
* The job-running and inspection controls, shared by both contract runners. Each job runs
|
|
8
|
+
* as its run's principal through whatever transport `connect` uses.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sliceBControls(engine: () => Engine, app: () => TestApp, connect: (principal: ActingAs) => Promise<QuadSdk>, onClock?: () => Promise<void>): SliceBControl;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { executeRun } from "../src/jobs/define.js";
|
|
2
|
+
/**
|
|
3
|
+
* The job-running and inspection controls, shared by both contract runners. Each job runs
|
|
4
|
+
* as its run's principal through whatever transport `connect` uses.
|
|
5
|
+
*/
|
|
6
|
+
export function sliceBControls(engine, app, connect, onClock = async () => { }) {
|
|
7
|
+
const appId = () => app().manifest.id;
|
|
8
|
+
return {
|
|
9
|
+
async advanceClock(ms) {
|
|
10
|
+
engine().advanceClock(ms);
|
|
11
|
+
await onClock();
|
|
12
|
+
},
|
|
13
|
+
async runDueJobs() {
|
|
14
|
+
const started = await engine().startDueRuns(appId());
|
|
15
|
+
return Promise.all(started.map(async (run) => {
|
|
16
|
+
// Each run acts as its own principal: cron as the system, scheduled jobs as whoever scheduled them (D7).
|
|
17
|
+
const sdk = await connect(run.runAs);
|
|
18
|
+
const result = await executeRun(app().jobs ?? [], run, sdk);
|
|
19
|
+
await engine().finishRun(appId(), run.runId, result);
|
|
20
|
+
return {
|
|
21
|
+
runId: run.runId,
|
|
22
|
+
jobName: run.jobName,
|
|
23
|
+
scheduledFor: run.scheduledFor,
|
|
24
|
+
ok: result.ok,
|
|
25
|
+
};
|
|
26
|
+
}));
|
|
27
|
+
},
|
|
28
|
+
redeliver: (runId) => engine().redeliverRun(appId(), runId),
|
|
29
|
+
runs: async () => (await engine().runs(appId())).map(({ id, jobName, scheduledFor, status, attempts, deliveries }) => ({
|
|
30
|
+
id,
|
|
31
|
+
jobName,
|
|
32
|
+
scheduledFor,
|
|
33
|
+
status,
|
|
34
|
+
attempts,
|
|
35
|
+
deliveries,
|
|
36
|
+
})),
|
|
37
|
+
inbox: async (personId) => engine().inbox(appId(), personId),
|
|
38
|
+
auditLog: async () => engine().auditLog(appId()),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function listAll(collection: any, options?: Record<string, unknown>): Promise<any[]>;
|
|
2
|
+
export declare function expectError(promise: Promise<unknown>, code: string): Promise<Error>;
|
|
3
|
+
export declare const ids: (records: readonly {
|
|
4
|
+
id: string;
|
|
5
|
+
}[]) => string[];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any -- the suite drives the SDK untyped, like plain JS would */
|
|
2
|
+
import { expect } from 'vitest';
|
|
3
|
+
import { isQuadError } from "../src/index.js";
|
|
4
|
+
export async function listAll(collection, options = {}) {
|
|
5
|
+
const items = [];
|
|
6
|
+
let cursor;
|
|
7
|
+
do {
|
|
8
|
+
const page = await collection.list({ ...options, limit: 200, ...(cursor ? { cursor } : {}) });
|
|
9
|
+
items.push(...page.items);
|
|
10
|
+
cursor = page.nextCursor ?? undefined;
|
|
11
|
+
} while (cursor);
|
|
12
|
+
return items;
|
|
13
|
+
}
|
|
14
|
+
export async function expectError(promise, code) {
|
|
15
|
+
try {
|
|
16
|
+
await promise;
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
expect(isQuadError(e), `expected a QuadError, got ${String(e)}`).toBe(true);
|
|
20
|
+
expect(e.code).toBe(code);
|
|
21
|
+
return e;
|
|
22
|
+
}
|
|
23
|
+
throw new Error(`expected ${code}, but the call succeeded`);
|
|
24
|
+
}
|
|
25
|
+
export const ids = (records) => records.map((r) => r.id).sort();
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { defineContractSuite } from './suite.ts';
|
|
2
|
-
export type { ContractEnv, ContractFactory, TestApp } from './suite.ts';
|
|
3
|
-
export {
|
|
2
|
+
export type { ContractEnv, ContractFactory, ContractOptions, TestApp } from './suite.ts';
|
|
3
|
+
export { sliceBControls } from './harness.ts';
|
|
4
|
+
export { STANDALONE_APP } from './standalone-apps.ts';
|
|
5
|
+
export { ACCESS_APP, ACCOUNT_APP, AWARDS_APP, BASIC_APP, DEMOGRAPHICS_APP, HR_APP, JOBS_APP, TEST_APP, TEST_ACCOUNTS, WELLBEING_APP, slowGate, } from './apps.ts';
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { defineContractSuite } from "./suite.js";
|
|
2
|
-
export {
|
|
2
|
+
export { sliceBControls } from "./harness.js";
|
|
3
|
+
export { STANDALONE_APP } from "./standalone-apps.js";
|
|
4
|
+
export { ACCESS_APP, ACCOUNT_APP, AWARDS_APP, BASIC_APP, DEMOGRAPHICS_APP, HR_APP, JOBS_APP, TEST_APP, TEST_ACCOUNTS, WELLBEING_APP, slowGate, } from "./apps.js";
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { type Manifest } from '@quad-education/schema';
|
|
2
|
+
import { type Job } from '../src/jobs/index.ts';
|
|
3
|
+
export declare const STANDALONE_SCHEMA: {
|
|
4
|
+
readonly post: {
|
|
5
|
+
readonly fields: {
|
|
6
|
+
readonly message: {
|
|
7
|
+
readonly kind: "text";
|
|
8
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
9
|
+
};
|
|
10
|
+
readonly tone: {
|
|
11
|
+
readonly kind: "enum";
|
|
12
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
13
|
+
} & {
|
|
14
|
+
values: readonly ["warm", "formal"];
|
|
15
|
+
};
|
|
16
|
+
readonly note: {
|
|
17
|
+
readonly kind: "text";
|
|
18
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
19
|
+
} & {
|
|
20
|
+
readonly optional: true;
|
|
21
|
+
};
|
|
22
|
+
readonly staffNote: {
|
|
23
|
+
readonly kind: "text";
|
|
24
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
25
|
+
} & {
|
|
26
|
+
readonly optional: true;
|
|
27
|
+
} & {
|
|
28
|
+
readonly visibleTo: readonly ["staff"];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
readonly indexes: readonly [readonly ["message"], readonly ["tone", "message"]];
|
|
32
|
+
readonly subject?: "note" | "message" | "tone" | "staffNote";
|
|
33
|
+
readonly access?: {
|
|
34
|
+
readonly Staff: {
|
|
35
|
+
readonly read: "all";
|
|
36
|
+
readonly create: "all";
|
|
37
|
+
readonly update: "all";
|
|
38
|
+
readonly delete: "all";
|
|
39
|
+
};
|
|
40
|
+
readonly Student: {
|
|
41
|
+
readonly read: "own";
|
|
42
|
+
readonly create: "own";
|
|
43
|
+
readonly update: "own";
|
|
44
|
+
readonly delete: "own";
|
|
45
|
+
};
|
|
46
|
+
readonly guest: {
|
|
47
|
+
readonly read: "own";
|
|
48
|
+
readonly create: "own";
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
readonly reply: {
|
|
53
|
+
readonly fields: {
|
|
54
|
+
readonly postId: {
|
|
55
|
+
readonly kind: "ref";
|
|
56
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
57
|
+
} & {
|
|
58
|
+
to: "post";
|
|
59
|
+
};
|
|
60
|
+
readonly message: {
|
|
61
|
+
readonly kind: "text";
|
|
62
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
readonly indexes: readonly [];
|
|
66
|
+
readonly subject?: "message" | "postId";
|
|
67
|
+
readonly access?: {
|
|
68
|
+
readonly Staff: {
|
|
69
|
+
readonly read: "all";
|
|
70
|
+
readonly create: "all";
|
|
71
|
+
};
|
|
72
|
+
readonly Student: {
|
|
73
|
+
readonly read: "own";
|
|
74
|
+
readonly create: "own";
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
/** No access block: only the system principal reaches it. */
|
|
79
|
+
readonly secret: {
|
|
80
|
+
readonly fields: {
|
|
81
|
+
readonly body: {
|
|
82
|
+
readonly kind: "text";
|
|
83
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
readonly indexes: readonly [];
|
|
87
|
+
readonly subject?: "body";
|
|
88
|
+
readonly access?: import("@quad-education/schema").AccessPolicy;
|
|
89
|
+
};
|
|
90
|
+
/** A staff-only log that test jobs write to, so tests can see what ran and as whom. */
|
|
91
|
+
readonly staffLog: {
|
|
92
|
+
readonly fields: {
|
|
93
|
+
readonly jobName: {
|
|
94
|
+
readonly kind: "text";
|
|
95
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
96
|
+
};
|
|
97
|
+
readonly runId: {
|
|
98
|
+
readonly kind: "text";
|
|
99
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
100
|
+
};
|
|
101
|
+
readonly scheduledFor: {
|
|
102
|
+
readonly kind: "text";
|
|
103
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
104
|
+
};
|
|
105
|
+
readonly principal: {
|
|
106
|
+
readonly kind: "text";
|
|
107
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
108
|
+
};
|
|
109
|
+
readonly note: {
|
|
110
|
+
readonly kind: "text";
|
|
111
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
112
|
+
} & {
|
|
113
|
+
readonly optional: true;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
readonly indexes: readonly [];
|
|
117
|
+
readonly subject?: "note" | "runId" | "jobName" | "scheduledFor" | "principal";
|
|
118
|
+
readonly access?: {
|
|
119
|
+
readonly Staff: {
|
|
120
|
+
readonly read: "all";
|
|
121
|
+
readonly create: "all";
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
/** What a job could see and do, recorded by the job itself (D7 run-as tests). */
|
|
126
|
+
readonly probe: {
|
|
127
|
+
readonly fields: {
|
|
128
|
+
readonly runId: {
|
|
129
|
+
readonly kind: "text";
|
|
130
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
131
|
+
};
|
|
132
|
+
readonly principal: {
|
|
133
|
+
readonly kind: "text";
|
|
134
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
135
|
+
};
|
|
136
|
+
readonly postsSeen: {
|
|
137
|
+
readonly kind: "number";
|
|
138
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
139
|
+
};
|
|
140
|
+
readonly otherPostVisible: {
|
|
141
|
+
readonly kind: "boolean";
|
|
142
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
143
|
+
};
|
|
144
|
+
readonly staffWriteAllowed: {
|
|
145
|
+
readonly kind: "boolean";
|
|
146
|
+
readonly tier: import("@quad-education/canonical").Tier;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
readonly indexes: readonly [];
|
|
150
|
+
readonly subject?: "runId" | "principal" | "postsSeen" | "otherPostVisible" | "staffWriteAllowed";
|
|
151
|
+
readonly access?: {
|
|
152
|
+
readonly Staff: {
|
|
153
|
+
readonly read: "all";
|
|
154
|
+
readonly create: "all";
|
|
155
|
+
};
|
|
156
|
+
readonly Student: {
|
|
157
|
+
readonly read: "own";
|
|
158
|
+
readonly create: "own";
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
type StandaloneApp = {
|
|
164
|
+
roles: 'staff' | 'student';
|
|
165
|
+
scopes: never;
|
|
166
|
+
notifications: 'test.message';
|
|
167
|
+
tasks: 'test.review';
|
|
168
|
+
scheduled: 'follow-up' | 'slow' | 'flaky' | 'probe' | 'chain';
|
|
169
|
+
schema: typeof STANDALONE_SCHEMA;
|
|
170
|
+
};
|
|
171
|
+
/** Lets a test hold the `slow` job mid-run, to prove runs of one job never overlap. */
|
|
172
|
+
export declare const standaloneSlowGate: {
|
|
173
|
+
release: () => void;
|
|
174
|
+
wait: Promise<void>;
|
|
175
|
+
reset(): void;
|
|
176
|
+
};
|
|
177
|
+
/** A version 1 standalone app with two app roles and guests, plus jobs and a weekly cron. */
|
|
178
|
+
export declare const STANDALONE_APP: {
|
|
179
|
+
manifest: Manifest;
|
|
180
|
+
schema: typeof STANDALONE_SCHEMA;
|
|
181
|
+
jobs: Job<StandaloneApp>[];
|
|
182
|
+
};
|
|
183
|
+
export {};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { boolean, defineSchema, entity, enumOf, number, ref, text, } from '@quad-education/schema';
|
|
2
|
+
import { defineJob } from "../src/jobs/index.js";
|
|
3
|
+
// The standalone app the contract suite runs everywhere, including against the data API, which has no school
|
|
4
|
+
// (V2c C1, D100). It mirrors what the school-persona sections check (data, access, notify, tasks, jobs, audit),
|
|
5
|
+
// with Quad accounts and guests as the people.
|
|
6
|
+
export const STANDALONE_SCHEMA = defineSchema({
|
|
7
|
+
post: entity({
|
|
8
|
+
fields: {
|
|
9
|
+
message: text(),
|
|
10
|
+
tone: enumOf(['warm', 'formal']),
|
|
11
|
+
note: text({ optional: true }),
|
|
12
|
+
staffNote: text({ optional: true, visibleTo: ['staff'] }),
|
|
13
|
+
},
|
|
14
|
+
// D37: sorts and range filters need an index.
|
|
15
|
+
indexes: [['message'], ['tone', 'message']],
|
|
16
|
+
access: {
|
|
17
|
+
Staff: { read: 'all', create: 'all', update: 'all', delete: 'all' },
|
|
18
|
+
Student: { read: 'own', create: 'own', update: 'own', delete: 'own' },
|
|
19
|
+
guest: { read: 'own', create: 'own' },
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
reply: entity({
|
|
23
|
+
fields: { postId: ref('post'), message: text() },
|
|
24
|
+
access: {
|
|
25
|
+
Staff: { read: 'all', create: 'all' },
|
|
26
|
+
Student: { read: 'own', create: 'own' },
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
/** No access block: only the system principal reaches it. */
|
|
30
|
+
secret: entity({ fields: { body: text() } }),
|
|
31
|
+
/** A staff-only log that test jobs write to, so tests can see what ran and as whom. */
|
|
32
|
+
staffLog: entity({
|
|
33
|
+
fields: {
|
|
34
|
+
jobName: text(),
|
|
35
|
+
runId: text(),
|
|
36
|
+
scheduledFor: text(),
|
|
37
|
+
principal: text(),
|
|
38
|
+
note: text({ optional: true }),
|
|
39
|
+
},
|
|
40
|
+
access: { Staff: { read: 'all', create: 'all' } },
|
|
41
|
+
}),
|
|
42
|
+
/** What a job could see and do, recorded by the job itself (D7 run-as tests). */
|
|
43
|
+
probe: entity({
|
|
44
|
+
fields: {
|
|
45
|
+
runId: text(),
|
|
46
|
+
principal: text(),
|
|
47
|
+
postsSeen: number(),
|
|
48
|
+
otherPostVisible: boolean(),
|
|
49
|
+
staffWriteAllowed: boolean(),
|
|
50
|
+
},
|
|
51
|
+
access: {
|
|
52
|
+
Staff: { read: 'all', create: 'all' },
|
|
53
|
+
Student: { read: 'own', create: 'own' },
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
/** Lets a test hold the `slow` job mid-run, to prove runs of one job never overlap. */
|
|
58
|
+
export const standaloneSlowGate = {
|
|
59
|
+
release: () => { },
|
|
60
|
+
wait: Promise.resolve(),
|
|
61
|
+
reset() {
|
|
62
|
+
this.wait = new Promise((resolve) => (this.release = resolve));
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const flakyCalls = new Map();
|
|
66
|
+
/** The idempotent pattern every job should follow (D20): derive keys from runId. */
|
|
67
|
+
async function logRun(ctx, note) {
|
|
68
|
+
const me = await ctx.sdk.auth.me();
|
|
69
|
+
await ctx.sdk.db.entity('staffLog').create({
|
|
70
|
+
jobName: ctx.jobName,
|
|
71
|
+
runId: ctx.runId,
|
|
72
|
+
scheduledFor: ctx.scheduledFor,
|
|
73
|
+
principal: me.kind,
|
|
74
|
+
...(note !== undefined ? { note } : {}),
|
|
75
|
+
}, { idempotencyKey: `log:${ctx.runId}` });
|
|
76
|
+
await ctx.sdk.audit.record({
|
|
77
|
+
action: 'job.logged',
|
|
78
|
+
summary: `${ctx.jobName} ran`,
|
|
79
|
+
idempotencyKey: `audit:${ctx.runId}`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const probe = defineJob({
|
|
83
|
+
name: 'probe',
|
|
84
|
+
async run({ sdk, runId, payload }) {
|
|
85
|
+
const me = await sdk.auth.me();
|
|
86
|
+
const posts = await sdk.db.entity('post').list({ limit: 200 });
|
|
87
|
+
const otherId = payload?.otherPostId;
|
|
88
|
+
const otherPostVisible = otherId
|
|
89
|
+
? await sdk.db
|
|
90
|
+
.entity('post')
|
|
91
|
+
.get(otherId)
|
|
92
|
+
.then(() => true, () => false)
|
|
93
|
+
: false;
|
|
94
|
+
const staffWriteAllowed = await sdk.db
|
|
95
|
+
.entity('staffLog')
|
|
96
|
+
.create({ jobName: 'probe', runId, scheduledFor: 'n/a', principal: me.kind }, { idempotencyKey: `probe-log:${runId}` })
|
|
97
|
+
.then(() => true, () => false);
|
|
98
|
+
await sdk.db.entity('probe').create({
|
|
99
|
+
runId,
|
|
100
|
+
principal: me.kind === 'account' ? `account:${me.account.id}` : me.kind,
|
|
101
|
+
postsSeen: posts.items.length,
|
|
102
|
+
otherPostVisible,
|
|
103
|
+
staffWriteAllowed,
|
|
104
|
+
}, { idempotencyKey: `probe:${runId}` });
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
const chain = defineJob({
|
|
108
|
+
name: 'chain',
|
|
109
|
+
async run({ sdk, runId, scheduledFor, payload }) {
|
|
110
|
+
await sdk.schedule.at({
|
|
111
|
+
job: 'probe',
|
|
112
|
+
when: scheduledFor,
|
|
113
|
+
payload,
|
|
114
|
+
idempotencyKey: `chain:${runId}`,
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
const STANDALONE_JOBS = [
|
|
119
|
+
probe,
|
|
120
|
+
chain,
|
|
121
|
+
defineJob({
|
|
122
|
+
name: 'weekly',
|
|
123
|
+
async run(ctx) {
|
|
124
|
+
await logRun(ctx);
|
|
125
|
+
await ctx.sdk.notify.send({
|
|
126
|
+
type: 'test.message',
|
|
127
|
+
to: { role: 'Staff' },
|
|
128
|
+
subject: 'Weekly summary',
|
|
129
|
+
body: 'Summary',
|
|
130
|
+
idempotencyKey: `notify:${ctx.runId}`,
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
defineJob({
|
|
135
|
+
name: 'follow-up',
|
|
136
|
+
async run(ctx) {
|
|
137
|
+
await logRun(ctx, JSON.stringify(ctx.payload));
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
defineJob({
|
|
141
|
+
name: 'slow',
|
|
142
|
+
async run(ctx) {
|
|
143
|
+
await standaloneSlowGate.wait;
|
|
144
|
+
await logRun(ctx);
|
|
145
|
+
},
|
|
146
|
+
}),
|
|
147
|
+
defineJob({
|
|
148
|
+
name: 'flaky',
|
|
149
|
+
async run(ctx) {
|
|
150
|
+
const calls = (flakyCalls.get(ctx.runId) ?? 0) + 1;
|
|
151
|
+
flakyCalls.set(ctx.runId, calls);
|
|
152
|
+
if (calls <= 2)
|
|
153
|
+
throw new Error(`flaky failure ${calls}`);
|
|
154
|
+
await logRun(ctx);
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
];
|
|
158
|
+
/** A version 1 standalone app with two app roles and guests, plus jobs and a weekly cron. */
|
|
159
|
+
export const STANDALONE_APP = {
|
|
160
|
+
manifest: {
|
|
161
|
+
manifestVersion: 1,
|
|
162
|
+
id: 'contract-standalone',
|
|
163
|
+
name: 'Standalone app',
|
|
164
|
+
sdk: '^0.2.0',
|
|
165
|
+
kit: 'react-ui',
|
|
166
|
+
roles: ['Staff', 'Student'],
|
|
167
|
+
schoolRoles: [],
|
|
168
|
+
guests: true,
|
|
169
|
+
scopes: [],
|
|
170
|
+
data: {},
|
|
171
|
+
notifications: ['test.message'],
|
|
172
|
+
tasks: ['test.review'],
|
|
173
|
+
scheduled: ['follow-up', 'slow', 'flaky', 'probe', 'chain'],
|
|
174
|
+
cron: [{ name: 'weekly', schedule: '0 7 * * 1', description: 'Mondays at 07:00' }],
|
|
175
|
+
},
|
|
176
|
+
schema: STANDALONE_SCHEMA,
|
|
177
|
+
jobs: STANDALONE_JOBS,
|
|
178
|
+
};
|