@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
|
@@ -1,709 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any -- the suite drives the SDK untyped, like plain JS would */
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import { TEST_ACCOUNTS } from "./apps.js";
|
|
4
|
-
import { expectError, ids, listAll } from "./helpers.js";
|
|
5
|
-
import { STANDALONE_APP, standaloneSlowGate } from "./standalone-apps.js";
|
|
6
|
-
// The standalone sections (V2c C1, D100): everything `sdk.db`, tasks, notify, schedule, cron, audit and the
|
|
7
|
-
// clock promise, checked with Quad accounts and guests on an app with no school. These are the sections the
|
|
8
|
-
// data API has to pass through its HTTP transport, because it never holds school data. They mirror the sections
|
|
9
|
-
// that use school personas, and run on every implementation.
|
|
10
|
-
const HOUR = 3_600_000;
|
|
11
|
-
const DAY = 24 * HOUR;
|
|
12
|
-
const APP_ID = STANDALONE_APP.manifest.id;
|
|
13
|
-
const account = (accountId, ...roles) => ({
|
|
14
|
-
kind: 'account',
|
|
15
|
-
accountId,
|
|
16
|
-
roles,
|
|
17
|
-
});
|
|
18
|
-
const EVE = account('acc-eve', 'Staff');
|
|
19
|
-
const GUS = account('acc-gus', 'Staff');
|
|
20
|
-
const CAI = account('acc-cai', 'Student');
|
|
21
|
-
const DEE = account('acc-dee', 'Student');
|
|
22
|
-
const GUEST1 = { kind: 'guest', guestId: 'guest-1' };
|
|
23
|
-
const SYSTEM = { kind: 'system', appId: APP_ID };
|
|
24
|
-
const review = { type: 'test.review', title: 'Review the notes' };
|
|
25
|
-
/** The accounts holding an app role, as the directory says (ids as notifications and tasks address them). */
|
|
26
|
-
const holders = (role) => TEST_ACCOUNTS.filter((a) => a.roles.includes(role))
|
|
27
|
-
.map((a) => `acct:${a.id}`)
|
|
28
|
-
.sort();
|
|
29
|
-
export function defineStandaloneSections(getEnv, useApp) {
|
|
30
|
-
const env = new Proxy({}, {
|
|
31
|
-
get: (_, key) => getEnv()[key],
|
|
32
|
-
});
|
|
33
|
-
describe('standalone: db', () => {
|
|
34
|
-
useApp(STANDALONE_APP);
|
|
35
|
-
it('creates, reads, updates and deletes records with system fields', async () => {
|
|
36
|
-
await env.control.setClock('2026-03-04T03:00:00.000Z');
|
|
37
|
-
const eve = await env.connect(EVE);
|
|
38
|
-
const posts = eve.db.entity('post');
|
|
39
|
-
const created = await posts.create({ message: 'Hello', tone: 'warm' });
|
|
40
|
-
expect(created).toMatchObject({
|
|
41
|
-
message: 'Hello',
|
|
42
|
-
tone: 'warm',
|
|
43
|
-
createdAt: '2026-03-04T03:00:00.000Z',
|
|
44
|
-
createdBy: 'acct:acc-eve',
|
|
45
|
-
updatedBy: 'acct:acc-eve',
|
|
46
|
-
});
|
|
47
|
-
expect('note' in created).toBe(false);
|
|
48
|
-
expect(await posts.get(created.id)).toEqual(created);
|
|
49
|
-
await env.control.setClock('2026-03-05T03:00:00.000Z');
|
|
50
|
-
const updated = await (await env.connect(GUS)).db
|
|
51
|
-
.entity('post')
|
|
52
|
-
.update(created.id, { tone: 'formal' });
|
|
53
|
-
expect(updated).toMatchObject({
|
|
54
|
-
tone: 'formal',
|
|
55
|
-
message: 'Hello',
|
|
56
|
-
createdBy: 'acct:acc-eve',
|
|
57
|
-
updatedBy: 'acct:acc-gus',
|
|
58
|
-
updatedAt: '2026-03-05T03:00:00.000Z',
|
|
59
|
-
});
|
|
60
|
-
await posts.delete(created.id);
|
|
61
|
-
await expectError(posts.get(created.id), 'NOT_FOUND');
|
|
62
|
-
await expectError(posts.update(created.id, { tone: 'warm' }), 'NOT_FOUND');
|
|
63
|
-
});
|
|
64
|
-
it('validates input against the schema', async () => {
|
|
65
|
-
const eve = await env.connect(EVE);
|
|
66
|
-
const posts = eve.db.entity('post');
|
|
67
|
-
await expectError(posts.create({ message: 'Hi' }), 'VALIDATION'); // missing tone
|
|
68
|
-
await expectError(posts.create({ message: 1, tone: 'warm' }), 'VALIDATION');
|
|
69
|
-
await expectError(posts.create({ message: 'Hi', tone: 'loud' }), 'VALIDATION');
|
|
70
|
-
await expectError(posts.create({ message: 'Hi', tone: 'warm', colour: 'red' }), 'VALIDATION');
|
|
71
|
-
await expectError(posts.create({ message: 'Hi', tone: 'warm', id: 'mine' }), 'VALIDATION');
|
|
72
|
-
await expectError(eve.db.entity('nope').list(), 'VALIDATION');
|
|
73
|
-
// Names inherited from Object.prototype are neither entities nor fields.
|
|
74
|
-
for (const name of ['toString', 'constructor', '__proto__']) {
|
|
75
|
-
await expectError(eve.db.entity(name).list(), 'VALIDATION');
|
|
76
|
-
}
|
|
77
|
-
await expectError(posts.create({ message: 'Hi', tone: 'warm', constructor: 'x' }), 'VALIDATION');
|
|
78
|
-
await expectError(posts.list({ where: { toString: 'x' } }), 'VALIDATION');
|
|
79
|
-
});
|
|
80
|
-
it('lists with where, orderBy and pagination', async () => {
|
|
81
|
-
const posts = (await env.connect(EVE)).db.entity('post');
|
|
82
|
-
for (let i = 0; i < 12; i++) {
|
|
83
|
-
await posts.create({
|
|
84
|
-
message: `m${String(i).padStart(2, '0')}`,
|
|
85
|
-
tone: i % 2 ? 'warm' : 'formal',
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
expect((await posts.list({ where: { tone: 'warm' }, limit: 200 })).items).toHaveLength(6);
|
|
89
|
-
const ordered = await posts.list({
|
|
90
|
-
orderBy: { field: 'message', direction: 'desc' },
|
|
91
|
-
limit: 5,
|
|
92
|
-
});
|
|
93
|
-
expect(ordered.items.map((g) => g.message)).toEqual(['m11', 'm10', 'm09', 'm08', 'm07']);
|
|
94
|
-
const next = await posts.list({
|
|
95
|
-
orderBy: { field: 'message', direction: 'desc' },
|
|
96
|
-
limit: 5,
|
|
97
|
-
cursor: ordered.nextCursor,
|
|
98
|
-
});
|
|
99
|
-
expect(next.items.map((g) => g.message)).toEqual(['m06', 'm05', 'm04', 'm03', 'm02']);
|
|
100
|
-
const last = await posts.list({
|
|
101
|
-
orderBy: { field: 'message', direction: 'desc' },
|
|
102
|
-
limit: 5,
|
|
103
|
-
cursor: next.nextCursor,
|
|
104
|
-
});
|
|
105
|
-
expect(last.items.map((g) => g.message)).toEqual(['m01', 'm00']);
|
|
106
|
-
expect(last.nextCursor).toBeNull();
|
|
107
|
-
await expectError(posts.list({ cursor: 'garbage' }), 'VALIDATION');
|
|
108
|
-
});
|
|
109
|
-
it('needs a declared index for orderBy and range filters (D37)', async () => {
|
|
110
|
-
const eve = await env.connect(EVE);
|
|
111
|
-
const posts = eve.db.entity('post');
|
|
112
|
-
for (let i = 0; i < 6; i++) {
|
|
113
|
-
await posts.create({
|
|
114
|
-
message: `m${i}`,
|
|
115
|
-
tone: i % 2 ? 'warm' : 'formal',
|
|
116
|
-
note: `n${i}`,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
const sort = await expectError(posts.list({ orderBy: { field: 'note' } }), 'VALIDATION');
|
|
120
|
-
expect(sort.message).toContain("indexes: [['note']]");
|
|
121
|
-
await expectError(posts.list({ where: { note: { gt: 'n2' } } }), 'VALIDATION');
|
|
122
|
-
await expectError(posts.list({
|
|
123
|
-
orderBy: [
|
|
124
|
-
{ field: 'tone', direction: 'asc' },
|
|
125
|
-
{ field: 'message', direction: 'desc' },
|
|
126
|
-
],
|
|
127
|
-
}), 'VALIDATION');
|
|
128
|
-
// Equality, in and ne work on any field.
|
|
129
|
-
expect((await posts.list({ where: { note: 'n3' } })).items).toHaveLength(1);
|
|
130
|
-
expect((await posts.list({ where: { note: { in: ['n1', 'n2'] } } })).items).toHaveLength(2);
|
|
131
|
-
expect((await posts.list({ where: { note: { ne: 'n0' } } })).items).toHaveLength(5);
|
|
132
|
-
// An index after an equality prefix, with a range on its next field.
|
|
133
|
-
const warm = await posts.list({
|
|
134
|
-
where: { tone: 'warm', message: { gte: 'm2' } },
|
|
135
|
-
orderBy: { field: 'message', direction: 'desc' },
|
|
136
|
-
});
|
|
137
|
-
expect(warm.items.map((g) => g.message)).toEqual(['m5', 'm3']);
|
|
138
|
-
// id, createdAt and createdBy are always indexed, even on an entity that declares no indexes.
|
|
139
|
-
const replies = eve.db.entity('reply');
|
|
140
|
-
const post = warm.items[0];
|
|
141
|
-
const first = await replies.create({ postId: post.id, message: 'first' });
|
|
142
|
-
await replies.create({ postId: post.id, message: 'second' });
|
|
143
|
-
expect((await replies.list({ orderBy: { field: 'createdAt', direction: 'desc' } })).items).toHaveLength(2);
|
|
144
|
-
expect((await replies.list({ where: { createdAt: { gte: first.createdAt } } })).items).toHaveLength(2);
|
|
145
|
-
expect((await replies.list({
|
|
146
|
-
where: { createdBy: 'acct:acc-eve' },
|
|
147
|
-
orderBy: { field: 'createdAt' },
|
|
148
|
-
})).items).toHaveLength(2);
|
|
149
|
-
await expectError(replies.list({ orderBy: { field: 'message' } }), 'VALIDATION');
|
|
150
|
-
});
|
|
151
|
-
it('includes refs, filtered by what the current user can read', async () => {
|
|
152
|
-
const eve = await env.connect(EVE);
|
|
153
|
-
const cai = await env.connect(CAI);
|
|
154
|
-
const ofEve = await eve.db.entity('post').create({ message: 'eve post', tone: 'warm' });
|
|
155
|
-
const ofCai = await cai.db.entity('post').create({ message: 'cai post', tone: 'warm' });
|
|
156
|
-
await eve.db.entity('reply').create({ postId: ofEve.id, message: 'from eve' });
|
|
157
|
-
await cai.db.entity('reply').create({ postId: ofCai.id, message: 'from cai' });
|
|
158
|
-
// A student can't reply to a post they can't read.
|
|
159
|
-
await expectError(cai.db.entity('reply').create({ postId: ofEve.id, message: 'nope' }), 'VALIDATION');
|
|
160
|
-
const asEve = await eve.db
|
|
161
|
-
.entity('reply')
|
|
162
|
-
.list({ include: ['postId'], orderBy: { field: 'createdAt' } });
|
|
163
|
-
expect(asEve.items.map((r) => r.$included.postId.id).sort()).toEqual([ofEve.id, ofCai.id].sort());
|
|
164
|
-
const asCai = await cai.db.entity('reply').list({ include: ['postId'] });
|
|
165
|
-
expect(asCai.items.map((r) => r.message)).toEqual(['from cai']);
|
|
166
|
-
expect(asCai.items[0].$included.postId.id).toBe(ofCai.id);
|
|
167
|
-
// Once the post is gone, an include on a reply that is still readable is null, not an error.
|
|
168
|
-
await eve.db.entity('post').delete(ofCai.id);
|
|
169
|
-
expect((await cai.db.entity('reply').list({ include: ['postId'] })).items[0].$included).toEqual({ postId: null });
|
|
170
|
-
await expectError(eve.db.entity('post').list({ include: ['message'] }), 'VALIDATION');
|
|
171
|
-
});
|
|
172
|
-
it('survives a restart (page reload)', async () => {
|
|
173
|
-
const created = await (await env.connect(EVE)).db
|
|
174
|
-
.entity('post')
|
|
175
|
-
.create({ message: 'kept', tone: 'warm' });
|
|
176
|
-
await env.control.restart();
|
|
177
|
-
expect((await (await env.connect(EVE)).db.entity('post').get(created.id)).message).toBe('kept');
|
|
178
|
-
});
|
|
179
|
-
it('drops repeated creates with the same idempotency key', async () => {
|
|
180
|
-
const posts = (await env.connect(EVE)).db.entity('post');
|
|
181
|
-
const a = await posts.create({ message: 'once', tone: 'warm' }, { idempotencyKey: 'k1' });
|
|
182
|
-
const b = await posts.create({ message: 'once', tone: 'warm' }, { idempotencyKey: 'k1' });
|
|
183
|
-
expect(b.id).toBe(a.id);
|
|
184
|
-
expect((await posts.list({ where: { message: 'once' } })).items).toHaveLength(1);
|
|
185
|
-
});
|
|
186
|
-
it('reset clears app data', async () => {
|
|
187
|
-
await (await env.connect(EVE)).db.entity('post').create({ message: 'x', tone: 'warm' });
|
|
188
|
-
await env.control.reset();
|
|
189
|
-
expect((await (await env.connect(EVE)).db.entity('post').list()).items).toEqual([]);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
describe('standalone: db access control (D25)', () => {
|
|
193
|
-
useApp(STANDALONE_APP);
|
|
194
|
-
async function seed() {
|
|
195
|
-
const eve = await env.connect(EVE);
|
|
196
|
-
const cai = await env.connect(CAI);
|
|
197
|
-
const ofEve = await eve.db
|
|
198
|
-
.entity('post')
|
|
199
|
-
.create({ message: 'staff', tone: 'warm', staffNote: 'private' });
|
|
200
|
-
const ofCai = await cai.db.entity('post').create({ message: 'student', tone: 'formal' });
|
|
201
|
-
return { eve, cai, ofEve, ofCai };
|
|
202
|
-
}
|
|
203
|
-
it("a student can't get, list, filter or page another person's records", async () => {
|
|
204
|
-
const { cai, ofEve, ofCai } = await seed();
|
|
205
|
-
const posts = cai.db.entity('post');
|
|
206
|
-
expect((await posts.list()).items.map((p) => p.id)).toEqual([ofCai.id]);
|
|
207
|
-
await expectError(posts.get(ofEve.id), 'NOT_FOUND');
|
|
208
|
-
expect((await posts.list({ where: { message: 'staff' } })).items).toEqual([]);
|
|
209
|
-
expect((await posts.list({ where: { id: ofEve.id } })).items).toEqual([]);
|
|
210
|
-
const page = await posts.list({ where: { tone: 'formal' }, limit: 1 });
|
|
211
|
-
expect(page.items.map((p) => p.id)).toEqual([ofCai.id]);
|
|
212
|
-
expect(page.nextCursor).toBeNull(); // the cursor doesn't hint that more records exist
|
|
213
|
-
await expectError(posts.update(ofEve.id, { tone: 'formal' }), 'NOT_FOUND');
|
|
214
|
-
await expectError(posts.delete(ofEve.id), 'NOT_FOUND');
|
|
215
|
-
});
|
|
216
|
-
it('NOT_FOUND for missing and inaccessible records alike, so existence never leaks', async () => {
|
|
217
|
-
const { cai, ofEve } = await seed();
|
|
218
|
-
const missing = await expectError(cai.db.entity('post').get('post_does-not-exist'), 'NOT_FOUND');
|
|
219
|
-
const hidden = await expectError(cai.db.entity('post').get(ofEve.id), 'NOT_FOUND');
|
|
220
|
-
expect(hidden.message.replace(ofEve.id, 'X')).toBe(missing.message.replace('post_does-not-exist', 'X'));
|
|
221
|
-
});
|
|
222
|
-
it('enforces field visibleTo on reads, filters and writes', async () => {
|
|
223
|
-
const { eve, cai, ofCai } = await seed();
|
|
224
|
-
// Staff can write the field on a student's post, and read it back; the student never receives it.
|
|
225
|
-
await eve.db.entity('post').update(ofCai.id, { staffNote: 'check in' });
|
|
226
|
-
expect((await eve.db.entity('post').get(ofCai.id)).staffNote).toBe('check in');
|
|
227
|
-
const asStudent = await cai.db.entity('post').get(ofCai.id);
|
|
228
|
-
expect('staffNote' in asStudent).toBe(false);
|
|
229
|
-
expect((await cai.db.entity('post').list()).items.every((p) => !('staffNote' in p))).toBe(true);
|
|
230
|
-
await expectError(cai.db.entity('post').list({ where: { staffNote: 'check in' } }), 'VALIDATION');
|
|
231
|
-
await expectError(cai.db.entity('post').update(ofCai.id, { staffNote: 'mine now' }), 'NOT_PERMITTED');
|
|
232
|
-
await expectError(cai.db.entity('post').create({ message: 'x', tone: 'warm', staffNote: 'x' }), 'NOT_PERMITTED');
|
|
233
|
-
const updated = await cai.db.entity('post').update(ofCai.id, { tone: 'warm' });
|
|
234
|
-
expect('staffNote' in updated).toBe(false);
|
|
235
|
-
expect((await eve.db.entity('post').get(ofCai.id)).staffNote).toBe('check in');
|
|
236
|
-
});
|
|
237
|
-
it('denies roles and operations the entity does not list', async () => {
|
|
238
|
-
const { eve, cai, ofEve } = await seed();
|
|
239
|
-
// secret has no access block, which means staff-only: an app role named Staff counts, any other doesn't.
|
|
240
|
-
await expectError(cai.db.entity('secret').list(), 'NOT_PERMITTED');
|
|
241
|
-
await expectError(cai.db.entity('secret').create({ body: 'x' }), 'NOT_PERMITTED');
|
|
242
|
-
await expectError((await env.connect(GUEST1)).db.entity('secret').list(), 'NOT_PERMITTED');
|
|
243
|
-
expect((await eve.db.entity('secret').list()).items).toEqual([]);
|
|
244
|
-
// reply: students may read and create, not update or delete.
|
|
245
|
-
const reply = await cai.db.entity('reply').create({
|
|
246
|
-
postId: (await cai.db.entity('post').list()).items[0].id,
|
|
247
|
-
message: 'hi',
|
|
248
|
-
});
|
|
249
|
-
await expectError(cai.db.entity('reply').update(reply.id, { message: 'edited' }), 'NOT_PERMITTED');
|
|
250
|
-
await expectError(cai.db.entity('reply').delete(reply.id), 'NOT_PERMITTED');
|
|
251
|
-
// staff may not update or delete replies (not listed).
|
|
252
|
-
await expectError(eve.db.entity('reply').update(reply.id, { message: 'x' }), 'NOT_PERMITTED');
|
|
253
|
-
// a guest may read and create their own posts, nothing more.
|
|
254
|
-
const guest = await env.connect(GUEST1);
|
|
255
|
-
const mine = await guest.db.entity('post').create({ message: 'guest', tone: 'warm' });
|
|
256
|
-
expect(mine.createdBy).toBe('guest:guest-1');
|
|
257
|
-
await expectError(guest.db.entity('post').update(mine.id, { tone: 'formal' }), 'NOT_PERMITTED');
|
|
258
|
-
await expectError(guest.db.entity('post').delete(mine.id), 'NOT_PERMITTED');
|
|
259
|
-
await expectError(guest.db.entity('reply').list(), 'NOT_PERMITTED');
|
|
260
|
-
expect(ids((await guest.db.entity('post').list()).items)).toEqual([mine.id]);
|
|
261
|
-
await expectError(guest.db.entity('post').get(ofEve.id), 'NOT_FOUND');
|
|
262
|
-
});
|
|
263
|
-
it("'own' covers only records the user created", async () => {
|
|
264
|
-
const { eve, cai, ofEve, ofCai } = await seed();
|
|
265
|
-
expect((await cai.db.entity('post').update(ofCai.id, { message: 'edited' })).message).toBe('edited');
|
|
266
|
-
await expectError(cai.db.entity('post').update(ofEve.id, { message: 'hijack' }), 'NOT_FOUND');
|
|
267
|
-
await cai.db.entity('post').delete(ofCai.id);
|
|
268
|
-
// Staff read everything, including a guest's.
|
|
269
|
-
const guest = await env.connect(GUEST1);
|
|
270
|
-
const g = await guest.db.entity('post').create({ message: 'guest', tone: 'warm' });
|
|
271
|
-
expect(ids(await listAll(eve.db.entity('post')))).toEqual(ids([ofEve, g]));
|
|
272
|
-
});
|
|
273
|
-
it('the system principal has full access to the app’s own data', async () => {
|
|
274
|
-
const { ofEve, ofCai } = await seed();
|
|
275
|
-
const system = await env.connect(SYSTEM);
|
|
276
|
-
const all = await system.db.entity('post').list();
|
|
277
|
-
expect(ids(all.items)).toEqual(ids([ofEve, ofCai]));
|
|
278
|
-
expect(all.items.some((p) => 'staffNote' in p)).toBe(true);
|
|
279
|
-
const secret = await system.db.entity('secret').create({ body: 'kept' });
|
|
280
|
-
expect(secret.createdBy).toBe('system:contract-standalone');
|
|
281
|
-
expect((await system.db.entity('secret').get(secret.id)).body).toBe('kept');
|
|
282
|
-
expect((await system.db.entity('post').update(ofCai.id, { message: 'edited by a job' })).message).toBe('edited by a job');
|
|
283
|
-
await system.db.entity('post').delete(ofEve.id);
|
|
284
|
-
expect((await system.db.entity('post').list()).items).toHaveLength(1);
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
describe('standalone: notify', () => {
|
|
288
|
-
useApp(STANDALONE_APP);
|
|
289
|
-
const send = (sdk, input) => sdk.notify.send({ type: 'test.message', subject: 'Subject', body: 'Body', ...input });
|
|
290
|
-
it('delivers to the simulated outbox, by account and by app role, once each', async () => {
|
|
291
|
-
const eve = await env.connect(EVE);
|
|
292
|
-
const result = await send(eve, {
|
|
293
|
-
to: [{ role: 'Student' }, { account: 'acc-cai' }, { account: 'acc-gus' }],
|
|
294
|
-
link: '/inbox',
|
|
295
|
-
});
|
|
296
|
-
const expected = [...new Set([...holders('Student'), 'acct:acc-gus'])].sort();
|
|
297
|
-
expect(result.recipients.map((r) => r.personId).sort()).toEqual(expected);
|
|
298
|
-
const outbox = await env.control.outbox();
|
|
299
|
-
expect(outbox.map((e) => e.recipientId).sort()).toEqual(expected);
|
|
300
|
-
expect(outbox[0]).toMatchObject({
|
|
301
|
-
appId: APP_ID,
|
|
302
|
-
type: 'test.message',
|
|
303
|
-
channel: 'inApp',
|
|
304
|
-
subject: 'Subject',
|
|
305
|
-
link: '/inbox',
|
|
306
|
-
sentBy: 'acct:acc-eve',
|
|
307
|
-
simulated: true,
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
it('rejects undeclared types, bad input and unknown recipients', async () => {
|
|
311
|
-
const eve = await env.connect(EVE);
|
|
312
|
-
await expectError(send(eve, { type: 'test.other', to: { role: 'Staff' } }), 'UNDECLARED_TYPE');
|
|
313
|
-
await expectError(send(eve, { to: [] }), 'VALIDATION');
|
|
314
|
-
await expectError(send(eve, { to: { role: 'Staff' }, subject: '' }), 'VALIDATION');
|
|
315
|
-
await expectError(send(eve, { to: { role: 'Staff' }, channel: 'sms' }), 'VALIDATION');
|
|
316
|
-
await expectError(send(eve, { to: { role: 'Staff' }, link: 'https://example.test' }), 'VALIDATION');
|
|
317
|
-
await expectError(send(eve, { to: { account: 'acc-nobody' } }), 'VALIDATION');
|
|
318
|
-
await expectError(send(eve, { to: { role: 'Janitor' } }), 'VALIDATION');
|
|
319
|
-
await expectError(send(eve, { to: 'everyone' }), 'VALIDATION');
|
|
320
|
-
expect(await env.control.outbox()).toEqual([]);
|
|
321
|
-
});
|
|
322
|
-
it('drops repeated sends with the same idempotency key', async () => {
|
|
323
|
-
const eve = await env.connect(EVE);
|
|
324
|
-
const first = await send(eve, { to: { account: 'acc-cai' }, idempotencyKey: 'once' });
|
|
325
|
-
const again = await send(eve, { to: { account: 'acc-cai' }, idempotencyKey: 'once' });
|
|
326
|
-
expect(again).toEqual(first);
|
|
327
|
-
expect(await env.control.outbox()).toHaveLength(1);
|
|
328
|
-
});
|
|
329
|
-
it('students can notify too, and a guest never can', async () => {
|
|
330
|
-
const cai = await env.connect(CAI);
|
|
331
|
-
expect((await send(cai, { to: { role: 'Staff' } })).recipients).toHaveLength(2);
|
|
332
|
-
await expectError(send(await env.connect(GUEST1), { to: { role: 'Staff' } }), 'FORBIDDEN');
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
describe('standalone: tasks (D6)', () => {
|
|
336
|
-
useApp(STANDALONE_APP);
|
|
337
|
-
it("mode 'each' gives every assignee their own task and inbox entry", async () => {
|
|
338
|
-
const eve = await env.connect(EVE);
|
|
339
|
-
const { tasks } = await eve.tasks.assign({ ...review, assignee: { role: 'Student' } });
|
|
340
|
-
const students = holders('Student');
|
|
341
|
-
expect(tasks).toHaveLength(students.length);
|
|
342
|
-
expect(new Set(tasks.map((t) => t.groupId)).size).toBe(1);
|
|
343
|
-
expect(tasks.flatMap((t) => t.assignees).sort()).toEqual(students);
|
|
344
|
-
// The creator sees who was assigned.
|
|
345
|
-
expect(tasks.every((t) => Array.isArray(t.assignees))).toBe(true);
|
|
346
|
-
for (const id of students)
|
|
347
|
-
expect(await env.control.inbox(id)).toHaveLength(1);
|
|
348
|
-
expect(await env.control.inbox('acct:acc-gus')).toEqual([]);
|
|
349
|
-
});
|
|
350
|
-
it("mode 'any' gives one shared task that closes when the first assignee completes it", async () => {
|
|
351
|
-
const eve = await env.connect(EVE);
|
|
352
|
-
const { tasks } = await eve.tasks.assign({
|
|
353
|
-
...review,
|
|
354
|
-
assignee: [{ account: 'acc-cai' }, { account: 'acc-dee' }],
|
|
355
|
-
mode: 'any',
|
|
356
|
-
});
|
|
357
|
-
expect(tasks).toHaveLength(1);
|
|
358
|
-
const done = await (await env.connect(CAI)).tasks.complete(tasks[0].id, { outcome: 'seen' });
|
|
359
|
-
expect(done).toMatchObject({
|
|
360
|
-
status: 'completed',
|
|
361
|
-
completedBy: 'acct:acc-cai',
|
|
362
|
-
outcome: 'seen',
|
|
363
|
-
assignedToMe: true,
|
|
364
|
-
});
|
|
365
|
-
expect(await env.control.inbox('acct:acc-dee')).toEqual([]);
|
|
366
|
-
await expectError((await env.connect(DEE)).tasks.complete(tasks[0].id), 'VALIDATION');
|
|
367
|
-
});
|
|
368
|
-
it('only an assignee can complete a task, never another account or the system principal', async () => {
|
|
369
|
-
const eve = await env.connect(EVE);
|
|
370
|
-
const { tasks } = await eve.tasks.assign({ ...review, assignee: { account: 'acc-cai' } });
|
|
371
|
-
const task = tasks[0];
|
|
372
|
-
await expectError(eve.tasks.complete(task.id), 'NOT_ASSIGNEE');
|
|
373
|
-
await expectError((await env.connect(SYSTEM)).tasks.complete(task.id), 'NOT_ASSIGNEE');
|
|
374
|
-
await expectError((await env.connect(DEE)).tasks.complete(task.id), 'NOT_FOUND');
|
|
375
|
-
const cai = await env.connect(CAI);
|
|
376
|
-
expect((await cai.tasks.list({ assignedToMe: true, status: 'open' })).items.map((t) => t.id)).toEqual([task.id]);
|
|
377
|
-
const done = await cai.tasks.complete(task.id, { outcome: 'ok' });
|
|
378
|
-
expect(done).toMatchObject({ status: 'completed', outcome: 'ok', assignedToMe: true });
|
|
379
|
-
expect('assignees' in done).toBe(false); // only the creator and the system see who was assigned
|
|
380
|
-
expect(await env.control.inbox('acct:acc-cai')).toEqual([]);
|
|
381
|
-
expect((await eve.tasks.get(task.id)).status).toBe('completed');
|
|
382
|
-
await expectError((await env.connect(DEE)).tasks.get(task.id), 'NOT_FOUND');
|
|
383
|
-
});
|
|
384
|
-
it('rejects undeclared task types, and a guest can not assign', async () => {
|
|
385
|
-
const eve = await env.connect(EVE);
|
|
386
|
-
await expectError(eve.tasks.assign({ ...review, type: 'test.nope', assignee: { role: 'Staff' } }), 'UNDECLARED_TYPE');
|
|
387
|
-
await expectError((await env.connect(GUEST1)).tasks.assign({ ...review, assignee: { role: 'Staff' } }), 'FORBIDDEN');
|
|
388
|
-
});
|
|
389
|
-
it('the creator or the system can cancel; an assignee cannot', async () => {
|
|
390
|
-
const eve = await env.connect(EVE);
|
|
391
|
-
const make = async () => (await eve.tasks.assign({ ...review, assignee: { account: 'acc-cai' } })).tasks[0];
|
|
392
|
-
const a = await make();
|
|
393
|
-
await expectError((await env.connect(CAI)).tasks.cancel(a.id), 'NOT_PERMITTED');
|
|
394
|
-
expect((await eve.tasks.cancel(a.id)).status).toBe('cancelled');
|
|
395
|
-
await expectError(eve.tasks.cancel(a.id), 'VALIDATION');
|
|
396
|
-
const b = await make();
|
|
397
|
-
expect((await (await env.connect(SYSTEM)).tasks.cancel(b.id)).status).toBe('cancelled');
|
|
398
|
-
expect(await env.control.inbox('acct:acc-cai')).toEqual([]);
|
|
399
|
-
});
|
|
400
|
-
it('drops repeated assigns with the same idempotency key, and survives a restart', async () => {
|
|
401
|
-
const eve = await env.connect(EVE);
|
|
402
|
-
const first = await eve.tasks.assign({
|
|
403
|
-
...review,
|
|
404
|
-
assignee: { account: 'acc-gus' },
|
|
405
|
-
idempotencyKey: 'once',
|
|
406
|
-
});
|
|
407
|
-
const again = await eve.tasks.assign({
|
|
408
|
-
...review,
|
|
409
|
-
assignee: { account: 'acc-gus' },
|
|
410
|
-
idempotencyKey: 'once',
|
|
411
|
-
});
|
|
412
|
-
expect(again.tasks.map((t) => t.id)).toEqual(first.tasks.map((t) => t.id));
|
|
413
|
-
await env.control.restart();
|
|
414
|
-
expect(await env.control.inbox('acct:acc-gus')).toHaveLength(1);
|
|
415
|
-
expect((await (await env.connect(EVE)).tasks.get(first.tasks[0].id)).title).toBe(review.title);
|
|
416
|
-
});
|
|
417
|
-
it('lists only the tasks an account made or was given', async () => {
|
|
418
|
-
const eve = await env.connect(EVE);
|
|
419
|
-
await eve.tasks.assign({ ...review, assignee: { account: 'acc-cai' } });
|
|
420
|
-
await (await env.connect(GUS)).tasks.assign({ ...review, assignee: { account: 'acc-dee' } });
|
|
421
|
-
expect((await eve.tasks.list()).items).toHaveLength(1);
|
|
422
|
-
expect((await (await env.connect(CAI)).tasks.list()).items).toHaveLength(1);
|
|
423
|
-
expect((await (await env.connect(DEE)).tasks.list()).items).toHaveLength(1);
|
|
424
|
-
expect((await (await env.connect(SYSTEM)).tasks.list()).items).toHaveLength(2);
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
describe('standalone: schedule and cron (D7, D8, D20)', () => {
|
|
428
|
-
useApp(STANDALONE_APP);
|
|
429
|
-
const logs = async () => (await (await env.connect(EVE)).db.entity('staffLog').list({ limit: 200 })).items;
|
|
430
|
-
it('runs a scheduled job at its time, not before, as the account that scheduled it (D7)', async () => {
|
|
431
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
432
|
-
const eve = await env.connect(EVE);
|
|
433
|
-
const s = await eve.schedule.at({
|
|
434
|
-
job: 'follow-up',
|
|
435
|
-
when: '2026-03-04T01:00:00.000Z',
|
|
436
|
-
payload: { postId: 'p1' },
|
|
437
|
-
});
|
|
438
|
-
expect(s).toMatchObject({
|
|
439
|
-
job: 'follow-up',
|
|
440
|
-
status: 'pending',
|
|
441
|
-
createdBy: 'acct:acc-eve',
|
|
442
|
-
runsAs: 'user',
|
|
443
|
-
});
|
|
444
|
-
expect(await env.control.runDueJobs()).toEqual([]);
|
|
445
|
-
await env.control.advanceClock(HOUR);
|
|
446
|
-
expect(await env.control.runDueJobs()).toEqual([
|
|
447
|
-
{
|
|
448
|
-
runId: `sched:${s.id}`,
|
|
449
|
-
jobName: 'follow-up',
|
|
450
|
-
scheduledFor: '2026-03-04T01:00:00.000Z',
|
|
451
|
-
ok: true,
|
|
452
|
-
},
|
|
453
|
-
]);
|
|
454
|
-
const [log] = await logs();
|
|
455
|
-
expect(log).toMatchObject({
|
|
456
|
-
jobName: 'follow-up',
|
|
457
|
-
principal: 'account',
|
|
458
|
-
note: '{"postId":"p1"}',
|
|
459
|
-
createdBy: 'acct:acc-eve',
|
|
460
|
-
});
|
|
461
|
-
expect((await eve.schedule.list()).map((x) => x.status)).toEqual(['done']);
|
|
462
|
-
expect(await env.control.runDueJobs()).toEqual([]);
|
|
463
|
-
});
|
|
464
|
-
it("a user-scheduled job can't read or write beyond the scheduling account's access (D7)", async () => {
|
|
465
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
466
|
-
const eve = await env.connect(EVE);
|
|
467
|
-
const other = await eve.db.entity('post').create({ message: 'staff only', tone: 'warm' });
|
|
468
|
-
const cai = await env.connect(CAI);
|
|
469
|
-
const s = await cai.schedule.at({
|
|
470
|
-
job: 'probe',
|
|
471
|
-
when: '2026-03-04T00:00:00Z',
|
|
472
|
-
payload: { otherPostId: other.id },
|
|
473
|
-
});
|
|
474
|
-
expect(s.runsAs).toBe('user');
|
|
475
|
-
expect((await env.control.runDueJobs())[0]?.ok).toBe(true);
|
|
476
|
-
const [probe] = (await eve.db.entity('probe').list()).items;
|
|
477
|
-
expect(probe).toMatchObject({
|
|
478
|
-
principal: 'account:acc-cai',
|
|
479
|
-
postsSeen: 0,
|
|
480
|
-
otherPostVisible: false,
|
|
481
|
-
staffWriteAllowed: false,
|
|
482
|
-
createdBy: 'acct:acc-cai',
|
|
483
|
-
});
|
|
484
|
-
expect(await logs()).toEqual([]);
|
|
485
|
-
});
|
|
486
|
-
it("a job scheduled from inside an account's job runs as that account too, never as the system", async () => {
|
|
487
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
488
|
-
await (await env.connect(CAI)).schedule.at({ job: 'chain', when: '2026-03-04T00:00:00Z' });
|
|
489
|
-
expect((await env.control.runDueJobs()).map((r) => r.jobName)).toEqual(['chain']);
|
|
490
|
-
expect((await env.control.runDueJobs()).map((r) => r.jobName)).toEqual(['probe']);
|
|
491
|
-
const [probe] = (await (await env.connect(EVE)).db.entity('probe').list()).items;
|
|
492
|
-
expect(probe).toMatchObject({ principal: 'account:acc-cai', staffWriteAllowed: false });
|
|
493
|
-
});
|
|
494
|
-
it('jobs scheduled by the system principal run as the system principal', async () => {
|
|
495
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
496
|
-
const eve = await env.connect(EVE);
|
|
497
|
-
for (const message of ['a', 'b', 'c'])
|
|
498
|
-
await eve.db.entity('post').create({ message, tone: 'warm' });
|
|
499
|
-
const s = await (await env.connect(SYSTEM)).schedule.at({
|
|
500
|
-
job: 'chain',
|
|
501
|
-
when: '2026-03-04T00:00:00Z',
|
|
502
|
-
});
|
|
503
|
-
expect(s).toMatchObject({ runsAs: 'system', createdBy: 'system:contract-standalone' });
|
|
504
|
-
await env.control.runDueJobs();
|
|
505
|
-
await env.control.runDueJobs();
|
|
506
|
-
const [probe] = (await eve.db.entity('probe').list()).items;
|
|
507
|
-
expect(probe).toMatchObject({ principal: 'system', postsSeen: 3, staffWriteAllowed: true });
|
|
508
|
-
});
|
|
509
|
-
it('any role the app serves can schedule; people see and cancel only their own', async () => {
|
|
510
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
511
|
-
const eve = await env.connect(EVE);
|
|
512
|
-
const cai = await env.connect(CAI);
|
|
513
|
-
const s = await cai.schedule.at({ job: 'follow-up', when: '2026-03-05T00:00:00Z' });
|
|
514
|
-
await eve.schedule.at({ job: 'follow-up', when: '2026-03-05T00:00:00Z' });
|
|
515
|
-
await expectError(eve.schedule.at({ job: 'weekly', when: '2026-03-04T01:00:00Z' }), 'UNDECLARED_TYPE');
|
|
516
|
-
await expectError(eve.schedule.at({ job: 'follow-up', when: 'tomorrow' }), 'VALIDATION');
|
|
517
|
-
expect((await cai.schedule.list()).map((x) => x.id)).toEqual([s.id]);
|
|
518
|
-
await expectError(eve.schedule.cancel(s.id), 'NOT_FOUND');
|
|
519
|
-
expect((await (await env.connect(SYSTEM)).schedule.list()).length).toBe(2);
|
|
520
|
-
expect((await cai.schedule.cancel(s.id)).status).toBe('cancelled');
|
|
521
|
-
await env.control.advanceClock(DAY);
|
|
522
|
-
expect((await env.control.runDueJobs()).length).toBe(1); // only staff's
|
|
523
|
-
});
|
|
524
|
-
it('cron follows the app’s timezone, and missed runs collapse into one catch-up run', async () => {
|
|
525
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z'); // Wednesday, 11:00 in Sydney
|
|
526
|
-
await env.control.restart(); // the cron baseline starts at this instant
|
|
527
|
-
const eve = await env.connect(EVE);
|
|
528
|
-
const [weekly] = await eve.cron.list();
|
|
529
|
-
// Monday 9 March 07:00 AEDT (UTC+11) is Sunday 20:00 UTC.
|
|
530
|
-
expect(weekly).toMatchObject({ name: 'weekly', nextRunAt: '2026-03-08T20:00:00.000Z' });
|
|
531
|
-
await env.control.advanceClock(21 * DAY); // three Mondays pass
|
|
532
|
-
expect(await env.control.runDueJobs()).toEqual([
|
|
533
|
-
{
|
|
534
|
-
runId: 'cron:weekly:2026-03-22T20:00:00.000Z',
|
|
535
|
-
jobName: 'weekly',
|
|
536
|
-
scheduledFor: '2026-03-22T20:00:00.000Z',
|
|
537
|
-
ok: true,
|
|
538
|
-
},
|
|
539
|
-
]);
|
|
540
|
-
expect(await env.control.runDueJobs()).toEqual([]);
|
|
541
|
-
expect((await env.control.outbox()).map((e) => e.recipientId).sort()).toEqual(holders('Staff'));
|
|
542
|
-
// After DST ends (5 April), Monday 07:00 AEST is 21:00 UTC.
|
|
543
|
-
expect((await eve.cron.list())[0].nextRunAt).toBe('2026-03-29T20:00:00.000Z');
|
|
544
|
-
await env.control.advanceClock(14 * DAY);
|
|
545
|
-
expect((await env.control.runDueJobs())[0]?.scheduledFor).toBe('2026-04-05T21:00:00.000Z');
|
|
546
|
-
});
|
|
547
|
-
it('never overlaps runs of the same job', async () => {
|
|
548
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
549
|
-
standaloneSlowGate.reset();
|
|
550
|
-
const eve = await env.connect(EVE);
|
|
551
|
-
await eve.schedule.at({ job: 'slow', when: '2026-03-04T00:00:00Z', idempotencyKey: 'a' });
|
|
552
|
-
await eve.schedule.at({ job: 'slow', when: '2026-03-04T00:00:00Z', idempotencyKey: 'b' });
|
|
553
|
-
const firstBatch = env.control.runDueJobs();
|
|
554
|
-
// Wait until the first run is under way. A real store takes a while to start it, and a second call made
|
|
555
|
-
// before that would start the same run again.
|
|
556
|
-
for (let i = 0; i < 400; i++) {
|
|
557
|
-
if ((await env.control.runs()).some((r) => r.status === 'running'))
|
|
558
|
-
break;
|
|
559
|
-
await new Promise((r) => setTimeout(r, 25));
|
|
560
|
-
}
|
|
561
|
-
expect(await env.control.runDueJobs()).toEqual([]); // the second waits while the first runs
|
|
562
|
-
expect((await env.control.runs()).map((r) => r.status).sort()).toEqual(['pending', 'running']);
|
|
563
|
-
standaloneSlowGate.release();
|
|
564
|
-
expect(await firstBatch).toHaveLength(1);
|
|
565
|
-
expect(await env.control.runDueJobs()).toHaveLength(1);
|
|
566
|
-
expect((await logs()).length).toBe(2);
|
|
567
|
-
});
|
|
568
|
-
it('retries a failing run, with the same runId, up to three attempts', async () => {
|
|
569
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
570
|
-
const s = await (await env.connect(EVE)).schedule.at({
|
|
571
|
-
job: 'flaky',
|
|
572
|
-
when: '2026-03-04T00:00:00Z',
|
|
573
|
-
});
|
|
574
|
-
expect((await env.control.runDueJobs())[0]?.ok).toBe(false);
|
|
575
|
-
expect((await env.control.runDueJobs())[0]?.ok).toBe(false);
|
|
576
|
-
expect((await env.control.runDueJobs())[0]).toMatchObject({
|
|
577
|
-
runId: `sched:${s.id}`,
|
|
578
|
-
ok: true,
|
|
579
|
-
});
|
|
580
|
-
expect((await env.control.runs())[0]).toMatchObject({ status: 'succeeded', attempts: 3 });
|
|
581
|
-
const outcomes = (await env.control.auditLog())
|
|
582
|
-
.filter((e) => e.action === 'job.run')
|
|
583
|
-
.map((e) => e.data.outcome);
|
|
584
|
-
expect(outcomes).toEqual(['failed', 'failed', 'succeeded']);
|
|
585
|
-
});
|
|
586
|
-
it('redelivery reuses the runId, and idempotency keys drop the repeats', async () => {
|
|
587
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
588
|
-
await env.control.restart();
|
|
589
|
-
await env.control.advanceClock(7 * DAY);
|
|
590
|
-
const [first] = await env.control.runDueJobs();
|
|
591
|
-
const counts = async () => ({
|
|
592
|
-
logs: (await logs()).length,
|
|
593
|
-
outbox: (await env.control.outbox()).length,
|
|
594
|
-
appAudit: (await env.control.auditLog()).filter((e) => e.source === 'app').length,
|
|
595
|
-
});
|
|
596
|
-
const before = await counts();
|
|
597
|
-
expect(before.logs).toBe(1);
|
|
598
|
-
await env.control.redeliver(first.runId);
|
|
599
|
-
const [again] = await env.control.runDueJobs();
|
|
600
|
-
expect(again).toMatchObject({ runId: first.runId, ok: true });
|
|
601
|
-
expect(await counts()).toEqual(before);
|
|
602
|
-
expect((await env.control.runs()).find((r) => r.id === first.runId).deliveries).toBe(2);
|
|
603
|
-
expect((await env.control.auditLog()).some((e) => e.action === 'job.redelivered')).toBe(true);
|
|
604
|
-
});
|
|
605
|
-
});
|
|
606
|
-
describe('standalone: audit (D19)', () => {
|
|
607
|
-
useApp(STANDALONE_APP);
|
|
608
|
-
it('stamps actor and time on app entries, and won’t let the app set them', async () => {
|
|
609
|
-
await env.control.setClock('2026-03-04T03:00:00.000Z');
|
|
610
|
-
const eve = await env.connect(EVE);
|
|
611
|
-
const entry = await eve.audit.record({
|
|
612
|
-
action: 'post.approved',
|
|
613
|
-
summary: 'Approved',
|
|
614
|
-
entity: 'post',
|
|
615
|
-
recordId: 'p1',
|
|
616
|
-
});
|
|
617
|
-
expect(entry).toMatchObject({
|
|
618
|
-
source: 'app',
|
|
619
|
-
actor: 'acct:acc-eve',
|
|
620
|
-
at: '2026-03-04T03:00:00.000Z',
|
|
621
|
-
appId: APP_ID,
|
|
622
|
-
action: 'post.approved',
|
|
623
|
-
});
|
|
624
|
-
for (const forged of [
|
|
625
|
-
{ actor: 'someone-else' },
|
|
626
|
-
{ source: 'platform' },
|
|
627
|
-
{ at: '2020-01-01T00:00:00Z' },
|
|
628
|
-
]) {
|
|
629
|
-
await expectError(eve.audit.record({ action: 'x.y', summary: 's', ...forged }), 'VALIDATION');
|
|
630
|
-
}
|
|
631
|
-
await expectError(eve.audit.record({ action: 'Not An Action', summary: 's' }), 'VALIDATION');
|
|
632
|
-
const once = await eve.audit.record({ action: 'x.once', summary: 's', idempotencyKey: 'k' });
|
|
633
|
-
expect((await eve.audit.record({ action: 'x.once', summary: 's', idempotencyKey: 'k' })).id).toBe(once.id);
|
|
634
|
-
});
|
|
635
|
-
it('records platform entries for tasks, notifications and jobs', async () => {
|
|
636
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
637
|
-
const eve = await env.connect(EVE);
|
|
638
|
-
const { tasks } = await eve.tasks.assign({ ...review, assignee: { account: 'acc-cai' } });
|
|
639
|
-
await (await env.connect(CAI)).tasks.complete(tasks[0].id);
|
|
640
|
-
const other = await eve.tasks.assign({ ...review, assignee: { account: 'acc-cai' } });
|
|
641
|
-
await eve.tasks.cancel(other.tasks[0].id);
|
|
642
|
-
await eve.notify.send({
|
|
643
|
-
type: 'test.message',
|
|
644
|
-
to: { account: 'acc-cai' },
|
|
645
|
-
subject: 'S',
|
|
646
|
-
body: 'B',
|
|
647
|
-
});
|
|
648
|
-
await eve.schedule.at({ job: 'follow-up', when: '2026-03-04T00:00:00Z' });
|
|
649
|
-
await env.control.runDueJobs();
|
|
650
|
-
const log = await env.control.auditLog();
|
|
651
|
-
const actions = log.map((e) => e.action);
|
|
652
|
-
for (const a of [
|
|
653
|
-
'task.assigned',
|
|
654
|
-
'task.completed',
|
|
655
|
-
'task.cancelled',
|
|
656
|
-
'notification.sent',
|
|
657
|
-
'job.run',
|
|
658
|
-
])
|
|
659
|
-
expect(actions, a).toContain(a);
|
|
660
|
-
expect(log.every((e) => e.source === 'platform' || e.source === 'app')).toBe(true);
|
|
661
|
-
// Nothing about a school: no sensitive reads or parent suppressions exist here.
|
|
662
|
-
expect(actions.some((a) => a === 'school.sensitive-read' || a.endsWith('.suppressed'))).toBe(false);
|
|
663
|
-
});
|
|
664
|
-
it('audit.list returns app-recorded entries only, and an account sees its own', async () => {
|
|
665
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
666
|
-
const eve = await env.connect(EVE);
|
|
667
|
-
await eve.tasks.assign({ ...review, assignee: { account: 'acc-gus' } });
|
|
668
|
-
await eve.audit.record({ action: 'staff.note', summary: 'by staff' });
|
|
669
|
-
const cai = await env.connect(CAI);
|
|
670
|
-
await cai.audit.record({ action: 'student.note', summary: 'by student' });
|
|
671
|
-
expect((await env.control.auditLog()).some((e) => e.source === 'platform')).toBe(true);
|
|
672
|
-
const asEve = (await eve.audit.list({ limit: 200 })).items;
|
|
673
|
-
expect(asEve.map((e) => e.action)).toEqual(['staff.note']);
|
|
674
|
-
expect(asEve.every((e) => e.source === 'app' && !('visibility' in e))).toBe(true);
|
|
675
|
-
expect((await cai.audit.list()).items.map((e) => e.action)).toEqual(['student.note']);
|
|
676
|
-
const asSystem = (await (await env.connect(SYSTEM)).audit.list()).items;
|
|
677
|
-
expect(asSystem.map((e) => e.action)).toEqual(['staff.note', 'student.note']);
|
|
678
|
-
});
|
|
679
|
-
it('is append-only: the SDK can only add and list, and entries never change', async () => {
|
|
680
|
-
const eve = await env.connect(EVE);
|
|
681
|
-
expect(Object.keys(eve.audit).sort()).toEqual(['list', 'record']);
|
|
682
|
-
const entry = await eve.audit.record({ action: 'x.kept', summary: 'kept' });
|
|
683
|
-
await eve.audit.record({ action: 'x.later', summary: 'later' });
|
|
684
|
-
await env.control.restart();
|
|
685
|
-
const [same] = (await (await env.connect(EVE)).audit.list({ action: 'x.kept' })).items;
|
|
686
|
-
expect(same).toEqual(entry);
|
|
687
|
-
});
|
|
688
|
-
});
|
|
689
|
-
describe('standalone: clock (D14, D21)', () => {
|
|
690
|
-
useApp(STANDALONE_APP);
|
|
691
|
-
it('advances a frozen clock, and follows it', async () => {
|
|
692
|
-
await env.control.setClock('2026-03-04T00:00:00.000Z');
|
|
693
|
-
await env.control.advanceClock(DAY);
|
|
694
|
-
expect((await env.connect(EVE)).clock.now()).toBe('2026-03-05T00:00:00.000Z');
|
|
695
|
-
});
|
|
696
|
-
it('gives today in the app’s timezone, not the browser’s', async () => {
|
|
697
|
-
// 14:30 UTC on 4 March is 01:30 on 5 March in Sydney (AEDT, UTC+11).
|
|
698
|
-
await env.control.setClock('2026-03-04T14:30:00.000Z');
|
|
699
|
-
expect((await env.connect(EVE)).clock.today()).toBe('2026-03-05');
|
|
700
|
-
await env.control.setClock('2026-06-04T13:59:00.000Z'); // 23:59 AEST
|
|
701
|
-
expect((await env.connect(EVE)).clock.today()).toBe('2026-06-04');
|
|
702
|
-
});
|
|
703
|
-
it('runs in real time by default', async () => {
|
|
704
|
-
await env.control.setClock(null);
|
|
705
|
-
const drift = Math.abs(Date.parse((await env.connect(EVE)).clock.now()) - Date.now());
|
|
706
|
-
expect(drift).toBeLessThan(5000);
|
|
707
|
-
});
|
|
708
|
-
});
|
|
709
|
-
}
|