@quad-education/sdk 0.1.3 → 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 +2 -0
- package/dist/contract-tests/apps.d.ts +105 -0
- package/dist/contract-tests/apps.js +167 -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 +536 -44
- 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 +60 -22
- package/dist/src/engine/engine.js +281 -162
- 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
|
@@ -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
|
+
};
|