@quad-education/sdk 0.2.0 → 0.3.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 +3 -2
- package/dist/contract-tests/apps.d.ts +1 -80
- package/dist/contract-tests/apps.js +1 -57
- package/dist/contract-tests/harness.d.ts +2 -2
- package/dist/contract-tests/index.d.ts +2 -3
- package/dist/contract-tests/index.js +1 -2
- package/dist/contract-tests/suite.d.ts +3 -10
- package/dist/contract-tests/suite.js +19 -205
- package/dist/src/client/connect.d.ts +3 -13
- package/dist/src/client/connect.js +3 -21
- package/dist/src/client/handshake-client.d.ts +0 -2
- package/dist/src/client/handshake-client.js +0 -2
- package/dist/src/contract/errors.d.ts +1 -1
- package/dist/src/contract/recipients.d.ts +2 -8
- package/dist/src/contract/types.d.ts +2 -18
- package/dist/src/contract/types.js +1 -1
- package/dist/src/engine/engine.d.ts +8 -42
- package/dist/src/engine/engine.js +34 -173
- package/dist/src/engine/host.d.ts +2 -2
- package/dist/src/engine/in-process.d.ts +2 -2
- package/dist/src/engine/runner.js +2 -4
- package/dist/src/engine/state.d.ts +6 -17
- package/dist/src/engine/state.js +3 -31
- package/dist/src/engine/tasks.js +3 -10
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/jobs/define.d.ts +2 -2
- package/dist/src/platform-types.d.ts +2 -2
- package/package.json +4 -4
- package/dist/contract-tests/standalone-apps.d.ts +0 -183
- package/dist/contract-tests/standalone-apps.js +0 -178
- package/dist/contract-tests/standalone.d.ts +0 -2
- package/dist/contract-tests/standalone.js +0 -709
- package/dist/src/client/http-transport.d.ts +0 -18
- package/dist/src/client/http-transport.js +0 -76
- package/dist/src/client/live.d.ts +0 -39
- package/dist/src/client/live.js +0 -122
- package/dist/src/contract/principals.d.ts +0 -30
- package/dist/src/contract/principals.js +0 -12
|
@@ -16,8 +16,6 @@ const ownsSchedule = (ctx, s) => ctx.principal.kind === 'system' || s.createdBy
|
|
|
16
16
|
export async function scheduleAt(k, ctx, input) {
|
|
17
17
|
// Anyone the app serves may schedule (the session already checked the role). The job
|
|
18
18
|
// runs as the caller, so it can never do more than the caller could (D7).
|
|
19
|
-
if (ctx.principal.kind === 'guest')
|
|
20
|
-
throw new QuadError('FORBIDDEN', 'guests cannot schedule jobs: nobody is signed in to run them as');
|
|
21
19
|
const job = str(input.job, 'job');
|
|
22
20
|
if (!ctx.app.manifest.scheduled.includes(job)) {
|
|
23
21
|
throw new QuadError('UNDECLARED_TYPE', `job '${job}' isn't in the manifest's scheduled list`);
|
|
@@ -67,7 +65,7 @@ export async function scheduleList(k, ctx, options) {
|
|
|
67
65
|
.map(scheduleView);
|
|
68
66
|
}
|
|
69
67
|
export async function cronList(k, ctx) {
|
|
70
|
-
const tz = k.timezone(
|
|
68
|
+
const tz = k.timezone();
|
|
71
69
|
const states = await k.store.allPlatform(ctx.app.manifest.id, 'cron');
|
|
72
70
|
return ctx.app.manifest.cron.map((job) => {
|
|
73
71
|
const next = nextOccurrence(job.schedule, tz, k.nowMs());
|
|
@@ -136,7 +134,7 @@ export async function startDueRuns(k, app) {
|
|
|
136
134
|
const state = await k.store.getPlatform(id, 'cron', job.name);
|
|
137
135
|
if (!state)
|
|
138
136
|
continue;
|
|
139
|
-
const latest = latestOccurrence(job.schedule, k.timezone(
|
|
137
|
+
const latest = latestOccurrence(job.schedule, k.timezone(), Date.parse(state.lastScheduledFor), now);
|
|
140
138
|
if (latest === null)
|
|
141
139
|
continue;
|
|
142
140
|
const scheduledFor = new Date(latest).toISOString();
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type Dataset, type Principal, type Role } from '@quad-education/canonical';
|
|
2
2
|
import type { AppSchema, Manifest } from '@quad-education/schema';
|
|
3
|
-
import type { ActingAs } from '../contract/principals.ts';
|
|
4
3
|
import type { AuditEntry, JsonValue, ScheduledRun, Task } from '../contract/types.ts';
|
|
5
4
|
import type { AppStore, PlatformKind } from './store.ts';
|
|
6
5
|
export type StoredTask = Omit<Task, 'assignedToMe' | 'assignees'> & {
|
|
@@ -19,7 +18,7 @@ export type StoredSchedule = Omit<ScheduledRun, 'runsAs'> & {
|
|
|
19
18
|
readonly appId: string;
|
|
20
19
|
status: ScheduledRun['status'];
|
|
21
20
|
/** Who the job runs as: whoever scheduled it, in their session (D7). */
|
|
22
|
-
readonly runAs:
|
|
21
|
+
readonly runAs: Principal;
|
|
23
22
|
};
|
|
24
23
|
export type RunStatus = 'pending' | 'running' | 'succeeded' | 'failed';
|
|
25
24
|
/** One delivery target of a job: a cron occurrence or a scheduled one-off (D7, D8). */
|
|
@@ -33,7 +32,7 @@ export type StoredRun = {
|
|
|
33
32
|
readonly scheduledFor: string;
|
|
34
33
|
readonly payload: JsonValue;
|
|
35
34
|
/** Cron runs as the system principal; a scheduled run as whoever scheduled it (D7). */
|
|
36
|
-
readonly runAs:
|
|
35
|
+
readonly runAs: Principal;
|
|
37
36
|
status: RunStatus;
|
|
38
37
|
attempts: number;
|
|
39
38
|
deliveries: number;
|
|
@@ -56,26 +55,16 @@ export type StoredAudit = AuditEntry & {
|
|
|
56
55
|
export type App = {
|
|
57
56
|
manifest: Manifest;
|
|
58
57
|
schema: AppSchema;
|
|
59
|
-
/** An app with no school has its own timezone (V2c); otherwise the school's. */
|
|
60
|
-
timezone?: string;
|
|
61
58
|
};
|
|
62
59
|
export type Ctx = {
|
|
63
60
|
app: App;
|
|
64
|
-
principal:
|
|
61
|
+
principal: Principal;
|
|
65
62
|
actorId: string;
|
|
66
|
-
/** Whether this call can reach a school: a school app, in an environment that has one (V2c, D100). */
|
|
67
|
-
school: boolean;
|
|
68
63
|
/** Students this principal can see, computed once per call. */
|
|
69
64
|
students?: 'all' | Set<string>;
|
|
70
65
|
};
|
|
71
66
|
export declare const isStaffOrSystem: (ctx: Ctx) => boolean;
|
|
72
|
-
|
|
73
|
-
* The school principal for a call that needs a school, or NOT_AVAILABLE: an account or a guest has no school,
|
|
74
|
-
* nor does a standalone app, nor an environment configured without one (V2c, D100).
|
|
75
|
-
*/
|
|
76
|
-
export declare function schoolPrincipal(ctx: Ctx): Principal;
|
|
77
|
-
/** The role names a principal acts under: its school role, an account's app roles, or `guest`. */
|
|
78
|
-
export declare function roleNamesOf(p: ActingAs): readonly string[];
|
|
67
|
+
export declare const roleOfPrincipal: (ctx: Ctx) => Role | "system";
|
|
79
68
|
/** Why a parent recipient was left out (D17). Recorded only in CIO-visible audit entries. */
|
|
80
69
|
export type SuppressedRecipient = {
|
|
81
70
|
personId: string;
|
|
@@ -85,7 +74,7 @@ export type SuppressedRecipient = {
|
|
|
85
74
|
export type ResolvedRecipients = {
|
|
86
75
|
recipients: {
|
|
87
76
|
personId: string;
|
|
88
|
-
role: Role
|
|
77
|
+
role: Role;
|
|
89
78
|
regarding?: string;
|
|
90
79
|
purpose?: 'correspondence' | 'consent';
|
|
91
80
|
}[];
|
|
@@ -97,7 +86,7 @@ export interface Kernel {
|
|
|
97
86
|
readonly store: AppStore;
|
|
98
87
|
nowIso(): string;
|
|
99
88
|
nowMs(): number;
|
|
100
|
-
timezone(
|
|
89
|
+
timezone(): string;
|
|
101
90
|
persistPlatform(app: App, kind: PlatformKind, items: readonly {
|
|
102
91
|
id: string;
|
|
103
92
|
}[]): Promise<void>;
|
package/dist/src/engine/state.js
CHANGED
|
@@ -1,31 +1,6 @@
|
|
|
1
1
|
import { visibleStudentIds, } from '@quad-education/canonical';
|
|
2
|
-
|
|
3
|
-
export const
|
|
4
|
-
(ctx.principal.kind === 'user' && ctx.principal.role === 'staff');
|
|
5
|
-
/**
|
|
6
|
-
* The school principal for a call that needs a school, or NOT_AVAILABLE: an account or a guest has no school,
|
|
7
|
-
* nor does a standalone app, nor an environment configured without one (V2c, D100).
|
|
8
|
-
*/
|
|
9
|
-
export function schoolPrincipal(ctx) {
|
|
10
|
-
const p = ctx.principal;
|
|
11
|
-
if (!ctx.school || p.kind === 'account' || p.kind === 'guest') {
|
|
12
|
-
throw new QuadError('NOT_AVAILABLE', 'there is no school here: this needs a school app running in a school');
|
|
13
|
-
}
|
|
14
|
-
return p;
|
|
15
|
-
}
|
|
16
|
-
/** The role names a principal acts under: its school role, an account's app roles, or `guest`. */
|
|
17
|
-
export function roleNamesOf(p) {
|
|
18
|
-
switch (p.kind) {
|
|
19
|
-
case 'user':
|
|
20
|
-
return [p.role];
|
|
21
|
-
case 'account':
|
|
22
|
-
return p.roles;
|
|
23
|
-
case 'guest':
|
|
24
|
-
return ['guest'];
|
|
25
|
-
default:
|
|
26
|
-
return [];
|
|
27
|
-
}
|
|
28
|
-
}
|
|
2
|
+
export const isStaffOrSystem = (ctx) => ctx.principal.kind === 'system' || ctx.principal.role === 'staff';
|
|
3
|
+
export const roleOfPrincipal = (ctx) => ctx.principal.kind === 'system' ? 'system' : ctx.principal.role;
|
|
29
4
|
/**
|
|
30
5
|
* Whether the current principal can see a subject student, under the school's visibility
|
|
31
6
|
* policy (D4). Tasks and audit entries about a student follow it for everyone, staff
|
|
@@ -34,10 +9,7 @@ export function roleNamesOf(p) {
|
|
|
34
9
|
export function seesSubject(k, ctx, studentId) {
|
|
35
10
|
if (studentId === undefined || ctx.principal.kind === 'system')
|
|
36
11
|
return true;
|
|
37
|
-
|
|
38
|
-
if (principal.kind === 'system')
|
|
39
|
-
return true;
|
|
40
|
-
ctx.students ??= visibleStudentIds(principal, k.dataset);
|
|
12
|
+
ctx.students ??= visibleStudentIds(ctx.principal, k.dataset);
|
|
41
13
|
return ctx.students === 'all'
|
|
42
14
|
? k.dataset.students.some((s) => s.id === studentId)
|
|
43
15
|
: ctx.students.has(studentId);
|
package/dist/src/engine/tasks.js
CHANGED
|
@@ -10,11 +10,8 @@ export function taskView(ctx, t) {
|
|
|
10
10
|
const { appId: _appId, purpose: _purpose, assignees, ...rest } = t;
|
|
11
11
|
return {
|
|
12
12
|
...rest,
|
|
13
|
-
assignedToMe: ctx.principal.kind
|
|
14
|
-
|
|
15
|
-
...(isStaffOrSystem(ctx) || (ctx.principal.kind === 'account' && t.createdBy === ctx.actorId)
|
|
16
|
-
? { assignees: [...assignees] }
|
|
17
|
-
: {}),
|
|
13
|
+
assignedToMe: ctx.principal.kind === 'user' && assignees.includes(ctx.actorId),
|
|
14
|
+
...(isStaffOrSystem(ctx) ? { assignees: [...assignees] } : {}),
|
|
18
15
|
};
|
|
19
16
|
}
|
|
20
17
|
/**
|
|
@@ -56,12 +53,8 @@ export async function assignTask(k, ctx, input) {
|
|
|
56
53
|
const dueAt = input.dueAt === undefined ? undefined : instant(input.dueAt, 'dueAt');
|
|
57
54
|
const link = appLink(input.link);
|
|
58
55
|
const data = jsonObject(input.data, 'data');
|
|
59
|
-
if (ctx.principal.kind === 'guest')
|
|
60
|
-
throw new QuadError('FORBIDDEN', 'guests cannot assign tasks: nobody is signed in to be accountable');
|
|
61
56
|
let regarding;
|
|
62
57
|
if (input.regarding !== undefined) {
|
|
63
|
-
if (!ctx.school)
|
|
64
|
-
throw new QuadError('NOT_AVAILABLE', 'regarding is for tasks about a student, which needs a school');
|
|
65
58
|
if (!isObject(input.regarding))
|
|
66
59
|
throw new QuadError('VALIDATION', 'regarding must be { studentId }');
|
|
67
60
|
regarding = str(input.regarding.studentId, 'regarding.studentId');
|
|
@@ -190,7 +183,7 @@ export async function completeTask(k, ctx, id, options) {
|
|
|
190
183
|
throw new QuadError('NOT_ASSIGNEE', 'only an assignee can complete this task, from the app');
|
|
191
184
|
}
|
|
192
185
|
// D17 is re-checked now, not just at assignment.
|
|
193
|
-
if (ctx.principal.
|
|
186
|
+
if (ctx.principal.role === 'parent' && t.regardingStudentId) {
|
|
194
187
|
if (!eligibleParents(k.dataset, t.regardingStudentId, t.purpose ?? 'correspondence').includes(ctx.actorId)) {
|
|
195
188
|
throw new QuadError('NOT_ASSIGNEE', 'you can no longer complete this task');
|
|
196
189
|
}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// @quad-education/sdk: the contract. Apps get an instance from `connect()` in '@quad-education/sdk/client'.
|
|
2
2
|
export * from "./contract/errors.js";
|
|
3
|
-
export * from "./contract/principals.js";
|
|
4
3
|
export * from "./contract/recipients.js";
|
|
5
4
|
export * from "./contract/types.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Principal } from '@quad-education/canonical';
|
|
2
2
|
import type { AppConfig, CurrentApp, JsonValue, QuadSdk } from '../contract/types.ts';
|
|
3
3
|
export type JobContext<A extends AppConfig = CurrentApp> = {
|
|
4
4
|
/**
|
|
@@ -29,7 +29,7 @@ export type RunRequest = {
|
|
|
29
29
|
readonly payload: JsonValue;
|
|
30
30
|
readonly attempt: number;
|
|
31
31
|
/** Who the run acts as. The host must connect the job's SDK as this principal (D7). */
|
|
32
|
-
readonly runAs:
|
|
32
|
+
readonly runAs: Principal;
|
|
33
33
|
};
|
|
34
34
|
export type RunResult = {
|
|
35
35
|
ok: true;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Role } from '@quad-education/canonical';
|
|
1
2
|
import type { Channel } from './contract/types.ts';
|
|
2
3
|
/** A simulated outbox entry (D10). Visible to the demo shell and tests, never to apps. */
|
|
3
4
|
export type OutboxEntry = {
|
|
@@ -6,8 +7,7 @@ export type OutboxEntry = {
|
|
|
6
7
|
readonly type: string;
|
|
7
8
|
readonly channel: Channel;
|
|
8
9
|
readonly recipientId: string;
|
|
9
|
-
|
|
10
|
-
readonly recipientRole: string;
|
|
10
|
+
readonly recipientRole: Role;
|
|
11
11
|
readonly subject: string;
|
|
12
12
|
readonly body: string;
|
|
13
13
|
readonly link?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quad-education/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"fake-indexeddb": "^6.2.5",
|
|
50
|
-
"@quad-education/
|
|
51
|
-
"@quad-education/
|
|
52
|
-
"@quad-education/schema": "^0.
|
|
50
|
+
"@quad-education/canonical": "^0.3.0",
|
|
51
|
+
"@quad-education/fictional-school": "^0.3.0",
|
|
52
|
+
"@quad-education/schema": "^0.3.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"vitest": "^5.0.3"
|
|
@@ -1,183 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,178 +0,0 @@
|
|
|
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
|
-
};
|