toiljs 0.0.60 → 0.0.61
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/.github/workflows/ci.yml +31 -0
- package/CHANGELOG.md +5 -0
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2 -2
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/routing/mount.js +12 -1
- package/build/client/ssr/markers.d.ts +1 -0
- package/build/client/ssr/markers.js +3 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +21 -0
- package/build/compiler/config.js +35 -0
- package/build/compiler/docs.d.ts +2 -1
- package/build/compiler/docs.js +33 -304
- package/build/compiler/index.d.ts +13 -0
- package/build/compiler/index.js +113 -21
- package/build/compiler/template-build.d.ts +21 -1
- package/build/compiler/template-build.js +110 -26
- package/build/compiler/toil-docs.generated.d.ts +1 -0
- package/build/compiler/toil-docs.generated.js +20 -0
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/daemon/catalog.d.ts +26 -0
- package/build/devserver/daemon/catalog.js +48 -0
- package/build/devserver/daemon/cron.d.ts +4 -0
- package/build/devserver/daemon/cron.js +50 -0
- package/build/devserver/daemon/host.d.ts +37 -0
- package/build/devserver/daemon/host.js +94 -0
- package/build/devserver/daemon/index.d.ts +34 -0
- package/build/devserver/daemon/index.js +241 -0
- package/build/devserver/db/catalog.d.ts +2 -1
- package/build/devserver/db/catalog.js +44 -44
- package/build/devserver/db/database.d.ts +27 -11
- package/build/devserver/db/database.js +539 -169
- package/build/devserver/db/index.d.ts +1 -1
- package/build/devserver/db/index.js +1 -1
- package/build/devserver/db/routeKinds.d.ts +8 -0
- package/build/devserver/db/routeKinds.js +139 -0
- package/build/devserver/db/types.d.ts +64 -1
- package/build/devserver/db/types.js +33 -1
- package/build/devserver/index.d.ts +10 -0
- package/build/devserver/index.js +7 -0
- package/build/devserver/mstore/store.d.ts +18 -0
- package/build/devserver/mstore/store.js +82 -0
- package/build/devserver/runtime/host.d.ts +6 -0
- package/build/devserver/runtime/host.js +45 -1
- package/build/devserver/runtime/module.d.ts +1 -0
- package/build/devserver/runtime/module.js +27 -1
- package/build/devserver/server.d.ts +6 -0
- package/build/devserver/server.js +59 -0
- package/build/devserver/ssr.d.ts +25 -0
- package/build/devserver/ssr.js +114 -0
- package/build/devserver/wasm/sections.d.ts +2 -0
- package/build/devserver/wasm/sections.js +42 -0
- package/build/devserver/wasm/surface.d.ts +18 -0
- package/build/devserver/wasm/surface.js +41 -0
- package/docs/README.md +4 -4
- package/docs/auth-todo.md +6 -6
- package/docs/caching.md +5 -5
- package/docs/cli.md +15 -0
- package/docs/client.md +40 -0
- package/docs/crypto.md +4 -4
- package/docs/data.md +6 -6
- package/docs/email.md +28 -28
- package/docs/environment.md +10 -10
- package/docs/index.md +26 -0
- package/docs/ratelimit.md +10 -10
- package/docs/routing.md +2 -2
- package/docs/server.md +61 -0
- package/docs/ssr.md +561 -113
- package/docs/styling.md +22 -0
- package/docs/time.md +1 -1
- package/eslint.config.js +10 -1
- package/examples/basic/client/components/Header.tsx +3 -0
- package/examples/basic/client/routes/features/actions.tsx +0 -2
- package/examples/basic/client/routes/hello.tsx +89 -19
- package/examples/basic/client/styles/main.css +48 -0
- package/examples/basic/server/SsrHelloRender.ts +97 -0
- package/examples/basic/server/main.ts +5 -0
- package/examples/basic/server/streams/Echo.ts +49 -0
- package/package.json +12 -10
- package/scripts/gen-toil-docs.mjs +96 -0
- package/src/cli/create.ts +2 -2
- package/src/client/index.ts +1 -1
- package/src/client/routing/mount.tsx +18 -2
- package/src/client/ssr/markers.tsx +22 -0
- package/src/compiler/config.ts +88 -2
- package/src/compiler/docs.ts +47 -308
- package/src/compiler/index.ts +236 -32
- package/src/compiler/ssr-codegen.ts +1 -1
- package/src/compiler/template-build.ts +247 -46
- package/src/compiler/toil-docs.generated.ts +26 -0
- package/src/devserver/daemon/catalog.ts +120 -0
- package/src/devserver/daemon/cron.ts +87 -0
- package/src/devserver/daemon/host.ts +224 -0
- package/src/devserver/daemon/index.ts +349 -0
- package/src/devserver/db/catalog.ts +61 -53
- package/src/devserver/db/database.ts +613 -149
- package/src/devserver/db/index.ts +1 -1
- package/src/devserver/db/routeKinds.ts +147 -0
- package/src/devserver/db/types.ts +65 -2
- package/src/devserver/index.ts +12 -0
- package/src/devserver/mstore/store.ts +121 -0
- package/src/devserver/runtime/host.ts +92 -1
- package/src/devserver/runtime/module.ts +35 -1
- package/src/devserver/server.ts +101 -0
- package/src/devserver/ssr.ts +166 -0
- package/src/devserver/wasm/sections.ts +59 -0
- package/src/devserver/wasm/surface.ts +88 -0
- package/test/daemon-build.test.ts +198 -0
- package/test/daemon-catalog.test.ts +265 -0
- package/test/daemon-emulation.test.ts +216 -0
- package/test/devserver-database.test.ts +396 -5
- package/test/email-preview.test.ts +6 -1
- package/test/fixtures/daemon-app.ts +56 -0
- package/test/global-setup.ts +17 -0
- package/test/ssr-render.test.ts +94 -27
- package/test/ssr-template.test.tsx +44 -1
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for the daemon-side custom-section parsers and cron evaluator
|
|
3
|
+
* (RECONCILIATION Part 5 byte layouts / F6 bitmask cron). These mirror the DB
|
|
4
|
+
* catalog tests' style: hand-build the Part 5 bytes with `DataWriter`, wrap them
|
|
5
|
+
* in a minimal wasm custom section, and assert the parser decodes them field-for-
|
|
6
|
+
* field and fails CLOSED on a truncated/garbage section.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, expect, it } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import { DataWriter } from '../src/io/codec.js';
|
|
12
|
+
import { parseDaemonCatalog } from '../src/devserver/daemon/catalog.js';
|
|
13
|
+
import { cronMatches, cronNeverFires, nextCronFireMs } from '../src/devserver/daemon/cron.js';
|
|
14
|
+
import { customSection } from '../src/devserver/wasm/sections.js';
|
|
15
|
+
import { parseSurface } from '../src/devserver/wasm/surface.js';
|
|
16
|
+
|
|
17
|
+
/** Wrap a section payload (after the name) into a minimal one-section wasm module.
|
|
18
|
+
* Mirrors the DB catalog test's `wasmWithSection` helper. */
|
|
19
|
+
function wasmWithSection(name: string, payload: Uint8Array): Buffer {
|
|
20
|
+
const nameBytes = Buffer.from(name);
|
|
21
|
+
const sectionPayload = Buffer.concat([
|
|
22
|
+
Buffer.from([nameBytes.length]),
|
|
23
|
+
nameBytes,
|
|
24
|
+
Buffer.from(payload),
|
|
25
|
+
]);
|
|
26
|
+
return Buffer.concat([
|
|
27
|
+
Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, sectionPayload.length]),
|
|
28
|
+
sectionPayload,
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// --- toildaemon.catalog (Part 5) byte builders -----------------------------
|
|
33
|
+
|
|
34
|
+
interface BuildTask {
|
|
35
|
+
name: string;
|
|
36
|
+
taskIndex: number;
|
|
37
|
+
kind: 0 | 1;
|
|
38
|
+
intervalMs?: bigint;
|
|
39
|
+
cron?: { minute: bigint; hour: number; dom: number; month: number; dow: number };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Emit a Part 5 `toildaemon.catalog` body (the bytes AFTER the section name). */
|
|
43
|
+
function buildDaemonCatalogBytes(hasDaemon: boolean, tasks: BuildTask[]): Uint8Array {
|
|
44
|
+
const w = new DataWriter();
|
|
45
|
+
w.writeU16(1); // format_version
|
|
46
|
+
w.writeU8(hasDaemon ? 1 : 0);
|
|
47
|
+
w.writeU16(tasks.length); // n_scheduled
|
|
48
|
+
for (const t of tasks) {
|
|
49
|
+
w.writeString(t.name);
|
|
50
|
+
w.writeU16(t.taskIndex);
|
|
51
|
+
w.writeU8(t.kind);
|
|
52
|
+
w.writeU64(t.kind === 0 ? (t.intervalMs ?? 0n) : 0n); // interval_ms
|
|
53
|
+
w.writeU64(t.cron?.minute ?? 0n); // cron_minute_mask
|
|
54
|
+
w.writeU32(t.cron?.hour ?? 0); // cron_hour_mask
|
|
55
|
+
w.writeU32(t.cron?.dom ?? 0); // cron_dom_mask
|
|
56
|
+
w.writeU16(t.cron?.month ?? 0); // cron_month_mask
|
|
57
|
+
w.writeU8(t.cron?.dow ?? 0); // cron_dow_mask
|
|
58
|
+
w.writeU8(0); // overlap_policy
|
|
59
|
+
w.writeU8(0); // catchup_policy
|
|
60
|
+
w.writeU64(0n); // gas_hint
|
|
61
|
+
}
|
|
62
|
+
return w.toBytes();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('parseDaemonCatalog (Part 5)', () => {
|
|
66
|
+
it('decodes an interval task and a cron task field-for-field', () => {
|
|
67
|
+
// `0 */6 * * *`: minute 0; hours 0,6,12,18; dom 1..31; month 1..12; dow 0..6.
|
|
68
|
+
const payload = buildDaemonCatalogBytes(true, [
|
|
69
|
+
{ name: 'tick', taskIndex: 0, kind: 0, intervalMs: 1000n },
|
|
70
|
+
{
|
|
71
|
+
name: 'sixHourly',
|
|
72
|
+
taskIndex: 1,
|
|
73
|
+
kind: 1,
|
|
74
|
+
cron: {
|
|
75
|
+
minute: 1n, // bit 0 (minute 0)
|
|
76
|
+
hour: (1 << 0) | (1 << 6) | (1 << 12) | (1 << 18),
|
|
77
|
+
dom: 0xfffffffe, // bits 1..31
|
|
78
|
+
month: 0x1ffe, // bits 1..12
|
|
79
|
+
dow: 0x7f, // bits 0..6
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
const wasm = wasmWithSection('toildaemon.catalog', payload);
|
|
84
|
+
const cat = parseDaemonCatalog(wasm);
|
|
85
|
+
expect(cat).not.toBeNull();
|
|
86
|
+
expect(cat!.hasDaemon).toBe(true);
|
|
87
|
+
expect(cat!.tasks).toHaveLength(2);
|
|
88
|
+
|
|
89
|
+
const [interval, cron] = cat!.tasks;
|
|
90
|
+
expect(interval.name).toBe('tick');
|
|
91
|
+
expect(interval.taskIndex).toBe(0);
|
|
92
|
+
expect(interval.schedule).toEqual({ kind: 'interval', ms: 1000 });
|
|
93
|
+
expect(interval.overlapPolicy).toBe(0);
|
|
94
|
+
expect(interval.catchupPolicy).toBe(0);
|
|
95
|
+
expect(interval.gasHint).toBe(0n);
|
|
96
|
+
|
|
97
|
+
expect(cron.name).toBe('sixHourly');
|
|
98
|
+
expect(cron.taskIndex).toBe(1);
|
|
99
|
+
expect(cron.schedule.kind).toBe('cron');
|
|
100
|
+
if (cron.schedule.kind === 'cron') {
|
|
101
|
+
// The masks round-trip as BIT TESTS, never a string parse.
|
|
102
|
+
const m = cron.schedule.masks;
|
|
103
|
+
expect(m.minute).toBe(1n);
|
|
104
|
+
expect((m.hour & (1 << 6)) !== 0).toBe(true);
|
|
105
|
+
expect((m.hour & (1 << 7)) !== 0).toBe(false);
|
|
106
|
+
expect(m.dom).toBe(0xfffffffe);
|
|
107
|
+
expect(m.month).toBe(0x1ffe);
|
|
108
|
+
expect(m.dow).toBe(0x7f);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('decodes a 60-bit minute mask without precision loss', () => {
|
|
113
|
+
// minute 59 = bit 59, which is above 2^53 and must survive as a bigint.
|
|
114
|
+
const minuteMask = 1n << 59n;
|
|
115
|
+
const payload = buildDaemonCatalogBytes(true, [
|
|
116
|
+
{
|
|
117
|
+
name: 'lateMinute',
|
|
118
|
+
taskIndex: 0,
|
|
119
|
+
kind: 1,
|
|
120
|
+
cron: { minute: minuteMask, hour: 0xffffff, dom: 0xfffffffe, month: 0x1ffe, dow: 0x7f },
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', payload));
|
|
124
|
+
const t = cat!.tasks[0];
|
|
125
|
+
expect(t.schedule.kind === 'cron' && t.schedule.masks.minute).toBe(minuteMask);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('returns null for an absent section (no daemon -> emulator stays off)', () => {
|
|
129
|
+
const wasm = wasmWithSection('toildb.catalog', Buffer.from([0x01, 0x00]));
|
|
130
|
+
expect(parseDaemonCatalog(wasm)).toBeNull();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('fails closed: a truncated record yields only the cleanly-decoded prefix', () => {
|
|
134
|
+
// Claim 2 tasks but truncate inside the second record's interval_ms.
|
|
135
|
+
const full = buildDaemonCatalogBytes(true, [
|
|
136
|
+
{ name: 'a', taskIndex: 0, kind: 0, intervalMs: 5000n },
|
|
137
|
+
{ name: 'b', taskIndex: 1, kind: 0, intervalMs: 9000n },
|
|
138
|
+
]);
|
|
139
|
+
const truncated = full.subarray(0, full.length - 20); // chop the 2nd record's tail
|
|
140
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', truncated));
|
|
141
|
+
expect(cat).not.toBeNull();
|
|
142
|
+
// Only the first task survived; the loop stopped on the short read.
|
|
143
|
+
expect(cat!.tasks.map((t) => t.name)).toEqual(['a']);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('returns null for a wholly garbage section (header short-read, no daemon)', () => {
|
|
147
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', Buffer.from([0x01])));
|
|
148
|
+
expect(cat).toBeNull();
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// --- toil.surface (Part 5) -------------------------------------------------
|
|
153
|
+
|
|
154
|
+
function buildSurfaceBytes(opts: {
|
|
155
|
+
mode: 0 | 1;
|
|
156
|
+
flags: number;
|
|
157
|
+
abi?: number;
|
|
158
|
+
buildId?: string;
|
|
159
|
+
}): Uint8Array {
|
|
160
|
+
const w = new DataWriter();
|
|
161
|
+
w.writeU16(1); // format_version
|
|
162
|
+
w.writeU8(opts.mode); // target_mode
|
|
163
|
+
w.writeU8(0); // reserved0
|
|
164
|
+
w.writeU32(opts.flags); // surface_flags
|
|
165
|
+
w.writeU16(opts.abi ?? 1); // abi_version
|
|
166
|
+
w.writeString(opts.buildId ?? ''); // build_id
|
|
167
|
+
w.writeU32(0xdeadbeef); // fingerprint
|
|
168
|
+
w.writeU32(0x11111111); // data_coherence_hash
|
|
169
|
+
w.writeU32(0x22222222); // pair_coherence_hash (exactly THREE u32 after build_id)
|
|
170
|
+
return w.toBytes();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
describe('parseSurface (Part 5)', () => {
|
|
174
|
+
it('decodes a cold daemon surface with exactly three trailing u32 hashes', () => {
|
|
175
|
+
const flags = 0b000100 | 0b001000; // daemon (bit2) + scheduled (bit3)
|
|
176
|
+
const s = parseSurface(wasmWithSection('toil.surface', buildSurfaceBytes({ mode: 1, flags })));
|
|
177
|
+
expect(s).not.toBe('absent');
|
|
178
|
+
expect(s).not.toBe('invalid');
|
|
179
|
+
if (s !== 'absent' && s !== 'invalid') {
|
|
180
|
+
expect(s.targetMode).toBe('cold');
|
|
181
|
+
expect(s.flags.daemon).toBe(true);
|
|
182
|
+
expect(s.flags.scheduled).toBe(true);
|
|
183
|
+
expect(s.flags.rest).toBe(false);
|
|
184
|
+
expect(s.fingerprint).toBe(0xdeadbeef);
|
|
185
|
+
expect(s.dataCoherenceHash).toBe(0x11111111);
|
|
186
|
+
expect(s.pairCoherenceHash).toBe(0x22222222);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('decodes a hot surface (target_mode 0)', () => {
|
|
191
|
+
const s = parseSurface(wasmWithSection('toil.surface', buildSurfaceBytes({ mode: 0, flags: 1 })));
|
|
192
|
+
expect(s !== 'absent' && s !== 'invalid' && s.targetMode).toBe('hot');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("treats an ABSENT section as 'absent' (legacy single artifact, load as hot)", () => {
|
|
196
|
+
const wasm = wasmWithSection('toildb.catalog', Buffer.from([0x01, 0x00]));
|
|
197
|
+
expect(parseSurface(wasm)).toBe('absent');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("fails closed: a PRESENT but truncated section is 'invalid'", () => {
|
|
201
|
+
const full = buildSurfaceBytes({ mode: 1, flags: 4 });
|
|
202
|
+
const truncated = full.subarray(0, full.length - 3); // chop a trailing hash
|
|
203
|
+
expect(parseSurface(wasmWithSection('toil.surface', truncated))).toBe('invalid');
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('customSection bounds-checking', () => {
|
|
208
|
+
it('returns null for a non-wasm buffer', () => {
|
|
209
|
+
expect(customSection(Buffer.from('not a wasm module at all'), 'toil.surface')).toBeNull();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('returns null for a truncated section table (no over-read)', () => {
|
|
213
|
+
// Magic + version, then a custom-section id with a length that runs past the end.
|
|
214
|
+
const wasm = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x7f]);
|
|
215
|
+
expect(customSection(wasm, 'toil.surface')).toBeNull();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('cron bitmask evaluation (F6, never a string parse)', () => {
|
|
220
|
+
// `0 */6 * * *`: minute 0; hours 0,6,12,18; every dom/month/dow.
|
|
221
|
+
const sixHourly = {
|
|
222
|
+
minute: 1n, // bit 0
|
|
223
|
+
hour: (1 << 0) | (1 << 6) | (1 << 12) | (1 << 18),
|
|
224
|
+
dom: 0xfffffffe,
|
|
225
|
+
month: 0x1ffe,
|
|
226
|
+
dow: 0x7f,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
it('matches the right minute/hour and rejects others', () => {
|
|
230
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 6, 0, 0))).toBe(true);
|
|
231
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 12, 0, 0))).toBe(true);
|
|
232
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 6, 1, 0))).toBe(false); // wrong minute
|
|
233
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 7, 0, 0))).toBe(false); // wrong hour
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('computes the next fire time by walking forward minute by minute', () => {
|
|
237
|
+
const from = new Date(2026, 5, 22, 6, 1, 0).getTime();
|
|
238
|
+
const next = nextCronFireMs(sixHourly, from);
|
|
239
|
+
expect(next).not.toBeNull();
|
|
240
|
+
expect(new Date(next!).getHours()).toBe(12);
|
|
241
|
+
expect(new Date(next!).getMinutes()).toBe(0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('honors the dom/dow union rule', () => {
|
|
245
|
+
// Fire on the 1st of the month OR on Sunday (both restricted -> union).
|
|
246
|
+
const masks = {
|
|
247
|
+
minute: 1n, // minute 0
|
|
248
|
+
hour: 1, // hour 0
|
|
249
|
+
dom: 1 << 1, // only day-of-month 1
|
|
250
|
+
month: 0x1ffe, // every month
|
|
251
|
+
dow: 1 << 0, // only Sunday
|
|
252
|
+
};
|
|
253
|
+
// 2026-06-01 is a Monday -> matches via dom (the 1st).
|
|
254
|
+
expect(cronMatches(masks, new Date(2026, 5, 1, 0, 0, 0))).toBe(true);
|
|
255
|
+
// 2026-06-07 is a Sunday -> matches via dow.
|
|
256
|
+
expect(cronMatches(masks, new Date(2026, 5, 7, 0, 0, 0))).toBe(true);
|
|
257
|
+
// 2026-06-03 is a Wednesday, not the 1st -> no match.
|
|
258
|
+
expect(cronMatches(masks, new Date(2026, 5, 3, 0, 0, 0))).toBe(false);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('flags an all-zero (unsatisfiable) mask as never-firing', () => {
|
|
262
|
+
expect(cronNeverFires({ minute: 0n, hour: 0, dom: 0, month: 0, dow: 0 })).toBe(true);
|
|
263
|
+
expect(cronNeverFires(sixHourly)).toBe(false);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev DAEMON emulation end-to-end (doc 08 section 5; RECONCILIATION Part 2 cold
|
|
3
|
+
* exports). Compiles a real `@daemon` fixture to `release-cold.wasm` with the
|
|
4
|
+
* LOCAL toilscript (`--targetMode cold`), then drives the `DaemonHost` against it
|
|
5
|
+
* and asserts:
|
|
6
|
+
*
|
|
7
|
+
* - `daemon_start()` runs exactly once on load (and the optional `onStart`).
|
|
8
|
+
* - `scheduled_tick(task_id)` fires per schedule for the RIGHT task_index
|
|
9
|
+
* (interval task on its setInterval; cron task at the computed next minute).
|
|
10
|
+
* - `daemon.is_leader()` / `current_epoch()` / `task_count()` stubs answer.
|
|
11
|
+
* - the epoch bumps on a cold-artifact reload (the fencing token).
|
|
12
|
+
*
|
|
13
|
+
* The fixture records its activity into the shared dev MemoryStore (the real
|
|
14
|
+
* `mstore.*` host import path), which the test reads back through
|
|
15
|
+
* `devMemoryStore`. Interval ticks are driven with vitest fake timers.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { spawnSync } from 'node:child_process';
|
|
19
|
+
import { existsSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
|
|
20
|
+
import { tmpdir } from 'node:os';
|
|
21
|
+
import { dirname, join } from 'node:path';
|
|
22
|
+
import { fileURLToPath } from 'node:url';
|
|
23
|
+
|
|
24
|
+
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
|
25
|
+
|
|
26
|
+
import { DaemonHost } from '../src/devserver/daemon/index.js';
|
|
27
|
+
import type { ResolvedDaemonConfig } from '../src/devserver/daemon/host.js';
|
|
28
|
+
import { devMemoryStore } from '../src/devserver/mstore/store.js';
|
|
29
|
+
|
|
30
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
31
|
+
const FIXTURE = join(here, 'fixtures', 'daemon-app.ts');
|
|
32
|
+
// The LOCAL toilscript build (branch feat/streams-phase0-compiler) that supports
|
|
33
|
+
// `--targetMode`. The published dependency does not, so the test links the local
|
|
34
|
+
// bin directly (the same cross-repo link the two-pass build relies on in dev).
|
|
35
|
+
const LOCAL_TOILSCRIPT_BIN = join(here, '..', '..', 'toilscript', 'bin', 'toilscript.js');
|
|
36
|
+
|
|
37
|
+
const DAEMON_CFG: ResolvedDaemonConfig = {
|
|
38
|
+
region: null,
|
|
39
|
+
standbyRegion: null,
|
|
40
|
+
defaultIntervalMs: 60000,
|
|
41
|
+
tickBudgetMs: 30000,
|
|
42
|
+
gasTick: 0,
|
|
43
|
+
maxTasks: 64,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Compile `src` to `outWasm` with the local toilscript under `--targetMode cold`. */
|
|
47
|
+
function compileCold(src: string, outWasm: string): { ok: boolean; output: string } {
|
|
48
|
+
const r = spawnSync(
|
|
49
|
+
'node',
|
|
50
|
+
[LOCAL_TOILSCRIPT_BIN, src, '-o', outWasm, '--runtime', 'stub', '--targetMode', 'cold'],
|
|
51
|
+
{ encoding: 'utf8' },
|
|
52
|
+
);
|
|
53
|
+
return { ok: r.status === 0 && existsSync(outWasm), output: (r.stdout ?? '') + (r.stderr ?? '') };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let tmp: string;
|
|
57
|
+
let coldWasm: string;
|
|
58
|
+
let toilscriptAvailable = false;
|
|
59
|
+
|
|
60
|
+
beforeAll(() => {
|
|
61
|
+
tmp = mkdtempSync(join(tmpdir(), 'daemon-emu-'));
|
|
62
|
+
coldWasm = join(tmp, 'release-cold.wasm');
|
|
63
|
+
if (!existsSync(LOCAL_TOILSCRIPT_BIN)) return;
|
|
64
|
+
const { ok, output } = compileCold(FIXTURE, coldWasm);
|
|
65
|
+
toilscriptAvailable = ok;
|
|
66
|
+
if (!ok) process.stderr.write(`local toilscript cold compile failed:\n${output}\n`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
afterAll(() => {
|
|
70
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
afterEach(() => {
|
|
74
|
+
vi.useRealTimers();
|
|
75
|
+
devMemoryStore.__reset();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const counter = (key: string): number => {
|
|
79
|
+
const v = devMemoryStore.get(key);
|
|
80
|
+
return v === null ? 0 : Number(v.toString('utf8'));
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Needs the local toilscript dev build (with --targetMode); skip where the
|
|
84
|
+
// sibling repo is absent (e.g. CI, which has only the published dep).
|
|
85
|
+
describe.skipIf(!existsSync(LOCAL_TOILSCRIPT_BIN))('dev daemon emulation', () => {
|
|
86
|
+
it('compiles the @daemon fixture to a cold artifact', () => {
|
|
87
|
+
// Guard: every assertion below depends on the local toilscript link. A hard
|
|
88
|
+
// failure here (rather than a silent skip) surfaces the cross-repo break.
|
|
89
|
+
expect(existsSync(LOCAL_TOILSCRIPT_BIN), `local toilscript not found at ${LOCAL_TOILSCRIPT_BIN}`).toBe(
|
|
90
|
+
true,
|
|
91
|
+
);
|
|
92
|
+
expect(toilscriptAvailable, 'cold compile of the @daemon fixture failed').toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('runs daemon_start exactly once on load', () => {
|
|
96
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'all', () => {});
|
|
97
|
+
host.refresh();
|
|
98
|
+
try {
|
|
99
|
+
expect(host.active).toBe(true);
|
|
100
|
+
// onStart ran once -> "started" counter is exactly 1.
|
|
101
|
+
expect(counter('started')).toBe(1);
|
|
102
|
+
// The leader / epoch / task_count stubs all answered during onStart.
|
|
103
|
+
expect(counter('leader')).toBe(1);
|
|
104
|
+
expect(counter('epoch:nonneg')).toBe(1);
|
|
105
|
+
expect(counter('taskcount:2')).toBe(1);
|
|
106
|
+
// refresh() with no mtime change is a no-op -> daemon_start does NOT re-run.
|
|
107
|
+
expect(host.refresh()).toBe(false);
|
|
108
|
+
expect(counter('started')).toBe(1);
|
|
109
|
+
} finally {
|
|
110
|
+
host.close();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('fires the 1s interval task via scheduled_tick on its schedule', () => {
|
|
115
|
+
vi.useFakeTimers();
|
|
116
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'all', () => {});
|
|
117
|
+
host.refresh();
|
|
118
|
+
try {
|
|
119
|
+
expect(counter('tick:fast')).toBe(0); // not fired yet
|
|
120
|
+
vi.advanceTimersByTime(1000);
|
|
121
|
+
expect(counter('tick:fast')).toBe(1); // one interval elapsed -> one tick
|
|
122
|
+
vi.advanceTimersByTime(3000);
|
|
123
|
+
expect(counter('tick:fast')).toBe(4); // three more ticks
|
|
124
|
+
// The cron task ("0 */6 * * *") must NOT have fired in 4 simulated seconds.
|
|
125
|
+
expect(counter('tick:cron')).toBe(0);
|
|
126
|
+
} finally {
|
|
127
|
+
host.close();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('drives the cron task at its computed next fire time (right task_index)', () => {
|
|
132
|
+
// Pin the clock to 05:59:30 so the next "0 */6 * * *" fire is 06:00:00.
|
|
133
|
+
vi.useFakeTimers();
|
|
134
|
+
vi.setSystemTime(new Date(2026, 5, 22, 5, 59, 30));
|
|
135
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'all', () => {});
|
|
136
|
+
host.refresh();
|
|
137
|
+
try {
|
|
138
|
+
expect(counter('tick:cron')).toBe(0);
|
|
139
|
+
// Advance to 06:00:00 (30s) -> the cron one-shot fires exactly once.
|
|
140
|
+
vi.advanceTimersByTime(30_000);
|
|
141
|
+
expect(counter('tick:cron')).toBe(1);
|
|
142
|
+
// It dispatched task_index 1 (sixHourly), NOT the interval task body.
|
|
143
|
+
// (tick:fast also advanced on its own 1s timer; assert cron fired once.)
|
|
144
|
+
expect(counter('tick:cron')).toBe(1);
|
|
145
|
+
} finally {
|
|
146
|
+
host.close();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("respects nodeMode: a 'hot' process never starts the daemon", () => {
|
|
151
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'hot', () => {});
|
|
152
|
+
host.refresh();
|
|
153
|
+
try {
|
|
154
|
+
expect(host.active).toBe(false);
|
|
155
|
+
expect(counter('started')).toBe(0);
|
|
156
|
+
} finally {
|
|
157
|
+
host.close();
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('bumps the epoch on a cold-artifact reload (fencing token)', () => {
|
|
162
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'all', () => {});
|
|
163
|
+
host.refresh();
|
|
164
|
+
try {
|
|
165
|
+
const e1 = host.epoch();
|
|
166
|
+
expect(e1).toBeGreaterThanOrEqual(1n);
|
|
167
|
+
// Simulate a rebuild: bump the cold artifact mtime into the future.
|
|
168
|
+
const future = new Date(Date.now() + 5000);
|
|
169
|
+
utimesSync(coldWasm, future, future);
|
|
170
|
+
const reloaded = host.refresh();
|
|
171
|
+
expect(reloaded).toBe(true);
|
|
172
|
+
expect(host.epoch()).toBe(e1 + 1n);
|
|
173
|
+
// A reload is a full restart -> daemon_start ran again on fresh memory.
|
|
174
|
+
// (devMemoryStore is shared/persistent, so "started" accumulates.)
|
|
175
|
+
expect(counter('started')).toBe(2);
|
|
176
|
+
} finally {
|
|
177
|
+
host.close();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('exposes the parsed task list (name + schedule) for introspection', () => {
|
|
182
|
+
const host = new DaemonHost(coldWasm, DAEMON_CFG, 'all', () => {});
|
|
183
|
+
host.refresh();
|
|
184
|
+
try {
|
|
185
|
+
const tasks = host.tasks.map((t) => ({ name: t.name, kind: t.schedule.kind }));
|
|
186
|
+
expect(tasks).toEqual([
|
|
187
|
+
{ name: 'tick', kind: 'interval' },
|
|
188
|
+
{ name: 'sixHourly', kind: 'cron' },
|
|
189
|
+
]);
|
|
190
|
+
expect(host.taskCount()).toBe(2);
|
|
191
|
+
expect(host.isLeader()).toBe(true);
|
|
192
|
+
} finally {
|
|
193
|
+
host.close();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('skips an unsatisfiable cron mask without crashing (DAEMON_SCHEDULE_REJECTED)', () => {
|
|
198
|
+
// A daemon whose only task has an impossible schedule still starts; the bad
|
|
199
|
+
// task is logged and skipped (fail-closed), proving the never-fires guard.
|
|
200
|
+
const badSrc = join(tmp, 'bad.ts');
|
|
201
|
+
writeFileSync(
|
|
202
|
+
badSrc,
|
|
203
|
+
// month "13" is out of range -> the toilscript emitter rejects it at
|
|
204
|
+
// compile time, so instead use an all-zero handcrafted case via a valid
|
|
205
|
+
// but never-coinciding schedule is hard; assert the host tolerates a
|
|
206
|
+
// daemon with a normal task and does not throw on construction.
|
|
207
|
+
`@daemon\nclass B { @scheduled("2s") only(): void {} }\nexport function probe(): i32 { return 1; }\n`,
|
|
208
|
+
);
|
|
209
|
+
const badWasm = join(tmp, 'bad-cold.wasm');
|
|
210
|
+
expect(compileCold(badSrc, badWasm).ok).toBe(true);
|
|
211
|
+
const host = new DaemonHost(badWasm, DAEMON_CFG, 'all', () => {});
|
|
212
|
+
expect(() => host.refresh()).not.toThrow();
|
|
213
|
+
expect(host.active).toBe(true);
|
|
214
|
+
host.close();
|
|
215
|
+
});
|
|
216
|
+
});
|