@lago-morph/prototype-framework-skill 0.1.0 → 0.2.0-rc02
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/package.json +1 -1
- package/skill/SKILL.md +19 -26
- package/skill/VERSION +1 -0
- package/skill/examples/notes-sql.md +1 -1
- package/skill/examples/secrets.md +20 -1
- package/skill/examples/tests.md +109 -0
- package/skill/reference/configuration.md +4 -4
- package/skill/reference/current-user.md +10 -10
- package/skill/reference/deploying.md +6 -8
- package/skill/reference/files.md +1 -1
- package/skill/reference/http-and-validation.md +1 -1
- package/skill/reference/kv.md +1 -1
- package/skill/reference/local-development.md +2 -2
- package/skill/reference/log.md +1 -1
- package/skill/reference/owner-tasks.md +53 -67
- package/skill/reference/secrets.md +3 -3
- package/skill/reference/sql-and-migrations.md +3 -3
- package/skill/reference/testing/e2e.md +44 -0
- package/skill/reference/testing/failures.md +44 -0
- package/skill/reference/testing/fuzzing.md +28 -0
- package/skill/reference/testing/test-changes.md +42 -0
- package/skill/reference/testing.md +33 -22
- package/skill/reference/troubleshooting.md +12 -15
- package/skill/templates/FRAMEWORK-ISSUE.md +1 -1
- package/skill/templates/README.md +1 -1
- package/skill/templates/production-stage.json +1 -3
package/package.json
CHANGED
package/skill/SKILL.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: prototype-framework
|
|
3
|
-
description: Build, test and deploy small web apps on the portable prototype framework (@lago-morph/prototype-framework). Use when creating or changing an app built from apps/template
|
|
3
|
+
description: Build, test and deploy small web apps on the portable prototype framework (@lago-morph/prototype-framework), and set up Cloudflare for them. Use when creating or changing an app built from apps/template; when its verify, smoke or deploy fails; when the user says "set up Cloudflare", "continue prototype setup" or "continue setup"; or on /prototype-framework … (for example /prototype-framework setup cloudflare, /prototype-framework continue setup).
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Building apps on the prototype framework
|
|
7
7
|
|
|
8
|
-
Apps are small (one or a few users, little data) and
|
|
8
|
+
Apps are small (one or a few users, little data) and run unchanged locally and on Cloudflare. The framework gives each request handler SQL, KV, files, configuration, secrets, a log and the current user; app code never sees which cloud it runs on. Package: `@lago-morph/prototype-framework` (server code), `/client` (browser code), `/testing` (tests). Commands: `npm run <command>` (flags after `--`), or `npm exec --no -- prototype-framework <command>`; never plain `npx`, which fetches a stranger's package of that name when the app is not installed.
|
|
9
|
+
|
|
10
|
+
**First step of every session:** run `npm run version-check`. It changes nothing; relay its suggestions as it prints them, and its "newer npm release" one only once per session. If the app is not installed yet (no `node_modules`), skip the check until `npm install` has run. Then run the git checks of [Cloudflare setup](#cloudflare-setup).
|
|
9
11
|
|
|
10
12
|
Read this page, then only the references your task needs (task map below).
|
|
11
13
|
|
|
@@ -13,9 +15,9 @@ Read this page, then only the references your task needs (task map below).
|
|
|
13
15
|
|
|
14
16
|
1. **Use only the framework's public exports**: `@lago-morph/prototype-framework` in server code, `/client` in browser code, `/testing` in tests, plus `hono` and `zod`. No platform SDKs, database drivers, Node built-ins or other framework subpaths.
|
|
15
17
|
2. **Never insert user text as HTML.** Build elements and set `textContent`; no `innerHTML` or other raw-HTML sinks.
|
|
16
|
-
3. **Pass `
|
|
18
|
+
3. **Pass `npm run verify` before declaring any work done**, including its e2e-api tests, and `npm run test:e2e:ui` when a page changed. Never skip or weaken a test, and never disable, suppress or weaken a check (`eslint-disable`, `@ts-ignore`, `any`, `as` casts, editing lint configuration).
|
|
17
19
|
4. **Claim an app works on a cloud only after its smoke test has passed on a deployed stage there** (`smoke --target <t> --stage test-…`). Local success says nothing about a cloud.
|
|
18
|
-
5. **Never change framework version pins**: the exact versions of `@lago-morph/prototype-framework`, `hono`, `zod`, `typescript`, `vite` and `
|
|
20
|
+
5. **Never change framework version pins**: the exact versions of `@lago-morph/prototype-framework`, `hono`, `zod`, `typescript`, `vite`, `vitest`, `@vitest/browser-playwright`, `@playwright/test` and `fast-check` in the app's `package.json`.
|
|
19
21
|
6. **Never work around the framework.** If you suspect a framework defect (a contract or reference that does not hold, a check that rejects code the references allow, a capability that is missing for the task), add an entry to `FRAMEWORK-ISSUE.md` at the app's root instead of patching, copying or bypassing framework code. Each entry states:
|
|
20
22
|
- the task attempted;
|
|
21
23
|
- the contract or reference consulted (file and section);
|
|
@@ -25,7 +27,14 @@ Read this page, then only the references your task needs (task map below).
|
|
|
25
27
|
|
|
26
28
|
Use [templates/FRAMEWORK-ISSUE.md](templates/FRAMEWORK-ISSUE.md).
|
|
27
29
|
7. **After recording an entry**, stop work on the blocked requirement; continue only work that does not depend on it; report the issue and the list of unfinished requirements to the user; and do not declare the app done.
|
|
28
|
-
8. **A
|
|
30
|
+
8. **A cloud run that cannot go ahead is not a framework issue** (Cloudflare not set up yet, a run paused, a quota used up). Do not add a `FRAMEWORK-ISSUE.md` entry for it; in your build record, list each check that needs it as `blocked` with the reason.
|
|
31
|
+
|
|
32
|
+
## Cloudflare setup
|
|
33
|
+
|
|
34
|
+
On "set up Cloudflare", "continue prototype setup" or "continue setup", run `npm run cloudflare-setup` and relay its output as is: links, template and next prompt unchanged. It does only what is missing, so run it again whenever asked. Exit status 1 with a message is a stop for the user, not a failure to work around. The flow: with no credential, setup prints a template and a token link; the owner puts the token and account ID in `~/.secrets/cloudflare` or the session's environment and says "continue prototype setup"; setup then sets the account up, never changing the owner's token. Steps and stops: [owner-tasks.md](reference/owner-tasks.md) §2.
|
|
35
|
+
|
|
36
|
+
- **Never open, print or search `.env.cloudflare` or `~/.secrets/cloudflare`** (or the file `CLOUDFLARE_SECRETS_FILE` names), and never echo `CLOUDFLARE_API_TOKEN` or `CLOUDFLARE_BOOTSTRAP_TOKEN`. The CLI reads them itself. Never ask for a token in the chat.
|
|
37
|
+
- **Git checks**, when a session starts work on the app and before every commit or push: `git check-ignore -q .env.cloudflare` and `git ls-files --error-unmatch .env.cloudflare`. Each time the file is not ignored (the first exits non-zero) or is tracked (the second exits 0), remind the user and point to [owner-tasks.md](reference/owner-tasks.md) §2. Never refuse to commit because of it.
|
|
29
38
|
|
|
30
39
|
## Task map
|
|
31
40
|
|
|
@@ -40,35 +49,19 @@ Read this page, then only the references your task needs (task map below).
|
|
|
40
49
|
| Add a per-stage setting | [configuration.md](reference/configuration.md) |
|
|
41
50
|
| Use an API key; sign or verify data | [secrets.md](reference/secrets.md) |
|
|
42
51
|
| Record what happened | [log.md](reference/log.md) |
|
|
43
|
-
| Test (in-process,
|
|
52
|
+
| Test (TDD, in-process, e2e, smoke, fuzz); a test or check fails; a test must change | [testing.md](reference/testing.md) and the file it names |
|
|
44
53
|
| Deploy, run the smoke test on a cloud, remove a stage | [deploying.md](reference/deploying.md) |
|
|
45
54
|
| Diagnose a failing request or deploy; a `verify` diagnostic | [troubleshooting.md](reference/troubleshooting.md), and the reference the diagnostic names |
|
|
46
|
-
|
|
|
55
|
+
| Set up Cloudflare; steps the owner performs (the one token, secrets, sign-in, admins, deploy and remove, logs) | [owner-tasks.md](reference/owner-tasks.md) |
|
|
47
56
|
|
|
48
57
|
Every `verify` diagnostic ends with `(read skill/reference/<file>.md)`; that file has a "Check rules that send you here" section for the rule.
|
|
49
58
|
|
|
50
|
-
## References
|
|
51
|
-
|
|
52
|
-
- [http-and-validation.md](reference/http-and-validation.md): routes, guards, validation, error codes, browser calls, HTML safety.
|
|
53
|
-
- [sql-and-migrations.md](reference/sql-and-migrations.md): tables, the portable query subset, `atomic`, migrations.
|
|
54
|
-
- [kv.md](reference/kv.md): key-value storage.
|
|
55
|
-
- [files.md](reference/files.md): tickets, the safe upload pattern, downloads.
|
|
56
|
-
- [secrets.md](reference/secrets.md): declaring, reading and setting secrets; HMAC signing.
|
|
57
|
-
- [configuration.md](reference/configuration.md): config keys and per-stage values files.
|
|
58
|
-
- [current-user.md](reference/current-user.md): levels, the admin role, login, login tests.
|
|
59
|
-
- [log.md](reference/log.md): writing entries and where they go.
|
|
60
|
-
- [local-development.md](reference/local-development.md): scaffolding, `dev`, local state.
|
|
61
|
-
- [testing.md](reference/testing.md): `createTestApp`, fake timers, the smoke test.
|
|
62
|
-
- [deploying.md](reference/deploying.md): stages, deploy, smoke, remove.
|
|
63
|
-
- [troubleshooting.md](reference/troubleshooting.md): finding log entries, error codes, login failures.
|
|
64
|
-
- [owner-tasks.md](reference/owner-tasks.md): the owner's copy-and-paste steps.
|
|
65
|
-
|
|
66
59
|
## Examples and templates
|
|
67
60
|
|
|
68
|
-
Examples are whole files copied unchanged from the
|
|
61
|
+
Examples are whole files copied unchanged from the template and the reference app, type-checked and tested in CI: [endpoint-and-page](examples/endpoint-and-page.md), [notes-sql](examples/notes-sql.md), [status-kv](examples/status-kv.md), [files-tickets](examples/files-tickets.md), [admin-and-current-user](examples/admin-and-current-user.md), [secrets](examples/secrets.md), [configuration](examples/configuration.md), [tests](examples/tests.md). Start from the template, not from any other app.
|
|
69
62
|
|
|
70
|
-
A new app starts from the template: `npm create @lago-morph/prototype-app@latest <dir>`, or `skill/scripts/scaffold <dir>` in
|
|
63
|
+
A new app starts from the template: `npm create @lago-morph/prototype-app@latest <dir>`, or `skill/scripts/scaffold <dir>` in the framework repository ([local-development.md](reference/local-development.md)); [templates/](templates/README.md) holds the files you fill in.
|
|
71
64
|
|
|
72
65
|
## Done means
|
|
73
66
|
|
|
74
|
-
`
|
|
67
|
+
`npm run verify` passes; every requirement is implemented and tested, each flow end to end ([testing.md](reference/testing.md)); for each cloud you claim, the smoke test passed on a deployed test stage there; and `FRAMEWORK-ISSUE.md` has no open entry blocking a requirement. Cloud runs that could not go ahead are listed as unfinished (`not-run` or `blocked`, hard rule 8), not raised as issues.
|
package/skill/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.2.0-rc02
|
|
@@ -53,7 +53,7 @@ import type { NoteList } from "../../shared/types";
|
|
|
53
53
|
import type { Env } from "../env";
|
|
54
54
|
import { notes } from "../schema";
|
|
55
55
|
|
|
56
|
-
const newNote = z.object({ text: z.string().trim().min(1).max(500) });
|
|
56
|
+
export const newNote = z.object({ text: z.string().trim().min(1).max(500) });
|
|
57
57
|
|
|
58
58
|
/** SQL and log: a logged-in user writes notes and lists their own. */
|
|
59
59
|
export function noteRoutes(api: Hono<Env>): void {
|
|
@@ -118,11 +118,14 @@ function toBase64url(bytes: Uint8Array): string {
|
|
|
118
118
|
function fromBase64url(text: string): Uint8Array<ArrayBuffer> | null {
|
|
119
119
|
if (!/^[A-Za-z0-9_-]+$/.test(text)) return null;
|
|
120
120
|
const base64 = text.replaceAll("-", "+").replaceAll("_", "/").padEnd(Math.ceil(text.length / 4) * 4, "=");
|
|
121
|
+
let bytes: Uint8Array<ArrayBuffer>;
|
|
121
122
|
try {
|
|
122
|
-
|
|
123
|
+
bytes = Uint8Array.from(atob(base64), (ch) => ch.charCodeAt(0));
|
|
123
124
|
} catch {
|
|
124
125
|
return null;
|
|
125
126
|
}
|
|
127
|
+
// atob ignores the unused low bits of the last character, so accept only the canonical spelling.
|
|
128
|
+
return toBase64url(bytes) === text ? bytes : null;
|
|
126
129
|
}
|
|
127
130
|
|
|
128
131
|
/** A token for `subject` that verifies until `expiresAt` (milliseconds since the epoch). */
|
|
@@ -177,6 +180,22 @@ describe("signed tokens (HMAC, base64url)", () => {
|
|
|
177
180
|
}
|
|
178
181
|
});
|
|
179
182
|
|
|
183
|
+
// root cause 2026-10-31-G-template-signing-noncanonical: a 32-byte signature is 43 base64url
|
|
184
|
+
// characters, so its last character carries 2 unused bits; only the issued spelling may verify.
|
|
185
|
+
it("accepts only the issued spelling of a signature, not one re-spelled in its last character", async () => {
|
|
186
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
187
|
+
const token = await signToken(key, "note-1", Date.now() + 60_000);
|
|
188
|
+
const [subject = "", expiresAt = "", signature = ""] = token.split(".");
|
|
189
|
+
expect(signature).toHaveLength(43);
|
|
190
|
+
const last = alphabet.indexOf(signature.slice(-1));
|
|
191
|
+
expect(last % 4, "the issued spelling leaves the unused low bits at zero").toBe(0);
|
|
192
|
+
for (const flip of [1, 2, 3]) {
|
|
193
|
+
const respelled = signature.slice(0, -1) + (alphabet[last ^ flip] ?? "");
|
|
194
|
+
expect(await verifyToken(key, `${subject}.${expiresAt}.${respelled}`), respelled.slice(-3)).toBeNull();
|
|
195
|
+
}
|
|
196
|
+
expect(await verifyToken(key, token)).toBe("note-1");
|
|
197
|
+
});
|
|
198
|
+
|
|
180
199
|
it("refuses a token once it has expired", async () => {
|
|
181
200
|
vi.useFakeTimers();
|
|
182
201
|
vi.setSystemTime(new Date("2026-01-01T00:00:00Z"));
|
package/skill/examples/tests.md
CHANGED
|
@@ -7,6 +7,7 @@ What it shows:
|
|
|
7
7
|
- `tests/app.test.ts`: `createTestApp` per test with `dispose()` afterwards; `mockUsers` with `as()` and the `user` option; `config` and `secrets` options; asserting status codes, error codes and `logs()`; an in-process upload sent with the ticket's wire form, to test the finalize route (the form is the same on every target; only `url` differs: here a path on the app, sent with `t.request`; on a cloud an absolute storage URL, which tests send with plain `fetch`, never a test helper, see "The smoke test" in [testing.md](../reference/testing.md)) (the HMAC tests are in [secrets.md](secrets.md)).
|
|
8
8
|
- `tests/smoke/smoke.test.ts`: one file that runs in-process under `verify` and against a deployed test stage under `smoke` (`connectTestStage()`).
|
|
9
9
|
- From the reference app, `tests/support.ts`: a shared `open()` helper that disposes test apps after each test.
|
|
10
|
+
- `tests/e2e/`: the e2e layer, against a local `dev` server that the template's `playwright.config.ts` has Playwright start and stop. `api/notes.spec.ts` drives one flow through the HTTP API (project `e2e-api`, no browser, run by `verify`) with its edge cases listed first; `ui/account.spec.ts` is the one browser test, of what only a page shows (project `e2e-ui`) ([e2e.md](../reference/testing/e2e.md)).
|
|
10
11
|
|
|
11
12
|
## `apps/template/tests/app.test.ts`
|
|
12
13
|
|
|
@@ -285,3 +286,111 @@ export interface ErrorBody {
|
|
|
285
286
|
readonly error: { readonly code: string; readonly message: string; readonly requestId: string };
|
|
286
287
|
}
|
|
287
288
|
```
|
|
289
|
+
|
|
290
|
+
## `apps/template/tests/e2e/api/notes.spec.ts`
|
|
291
|
+
|
|
292
|
+
<!-- example-source: apps/template/tests/e2e/api/notes.spec.ts -->
|
|
293
|
+
```ts
|
|
294
|
+
import { expect, test, type APIRequestContext, type APIResponse } from "@playwright/test";
|
|
295
|
+
import type { NoteList } from "../../../src/shared/types";
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The notes flow end to end, through the HTTP API of a local `dev` server: a person signs in,
|
|
299
|
+
* writes notes and lists their own. Most of a flow's e2e cases are API tests like these.
|
|
300
|
+
*
|
|
301
|
+
* Edge cases, listed before the tests (the skill's reference/testing/e2e.md):
|
|
302
|
+
* - Boundaries: empty text and text of only spaces are refused; 500 characters are accepted; 501 are refused.
|
|
303
|
+
* - Text: Unicode text comes back unchanged; surrounding spaces are trimmed. Case, `..`, `/`,
|
|
304
|
+
* percent-encoding and prefix names do not apply: a note is free text, never a name or a path.
|
|
305
|
+
* - Order and time: not asserted here. Two notes made in the same millisecond tie on their time and
|
|
306
|
+
* fall back to their random IDs; there is no expiry.
|
|
307
|
+
* - Access: anonymous is refused (401); another user does not see the notes. Admin does not apply:
|
|
308
|
+
* the notes routes treat an admin as any user, and there is no admin view of notes.
|
|
309
|
+
* - Repetition: the same text twice makes two notes. Concurrent writes do not apply: each request
|
|
310
|
+
* inserts one row with a new ID, and the notes contract promises nothing about concurrency.
|
|
311
|
+
* - Failures: an unavailable database is simulated in-process only (`setUnavailable`, tests/app.test.ts).
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** Signs `request` in as a fresh mock user (local data is kept between runs) and returns its ID. */
|
|
315
|
+
async function signIn(request: APIRequestContext): Promise<string> {
|
|
316
|
+
const id = `e2e-${crypto.randomUUID()}`;
|
|
317
|
+
const res = await request.post("/api/auth/mock-login", { data: { level: "user", id } });
|
|
318
|
+
expect(res.status()).toBe(204);
|
|
319
|
+
return id;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async function errorCode(res: APIResponse): Promise<string | undefined> {
|
|
323
|
+
const body: { error?: { code?: string } } = await res.json();
|
|
324
|
+
return body.error?.code;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
test("a signed-in user writes notes and lists only their own", async ({ request }) => {
|
|
328
|
+
await signIn(request);
|
|
329
|
+
const text = `Ünïcödé ✓ ${crypto.randomUUID()}`;
|
|
330
|
+
const created = await request.post("/api/notes", { data: { text: ` ${text} ` } });
|
|
331
|
+
expect(created.status()).toBe(201);
|
|
332
|
+
const again = await request.post("/api/notes", { data: { text } });
|
|
333
|
+
expect(again.status()).toBe(201);
|
|
334
|
+
|
|
335
|
+
const mineRes = await request.get("/api/notes");
|
|
336
|
+
expect(mineRes.status()).toBe(200);
|
|
337
|
+
const mine: NoteList = await mineRes.json();
|
|
338
|
+
expect(mine.notes.filter((n) => n.text === text)).toHaveLength(2);
|
|
339
|
+
|
|
340
|
+
// A new mock login replaces the session: now another user.
|
|
341
|
+
await signIn(request);
|
|
342
|
+
const theirsRes = await request.get("/api/notes");
|
|
343
|
+
expect(theirsRes.status()).toBe(200);
|
|
344
|
+
const theirs: NoteList = await theirsRes.json();
|
|
345
|
+
expect(theirs.notes.filter((n) => n.text === text)).toEqual([]);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test("anonymous requests are refused", async ({ request }) => {
|
|
349
|
+
const list = await request.get("/api/notes");
|
|
350
|
+
expect(list.status()).toBe(401);
|
|
351
|
+
expect(await errorCode(list)).toBe("AUTH.UNAUTHENTICATED");
|
|
352
|
+
const write = await request.post("/api/notes", { data: { text: "hello" } });
|
|
353
|
+
expect(write.status()).toBe(401);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("note text must be 1 to 500 characters after trimming", async ({ request }) => {
|
|
357
|
+
await signIn(request);
|
|
358
|
+
for (const text of ["", " ", "x".repeat(501)]) {
|
|
359
|
+
const res = await request.post("/api/notes", { data: { text } });
|
|
360
|
+
expect(res.status(), JSON.stringify(text.slice(0, 20))).toBe(400);
|
|
361
|
+
expect(await errorCode(res)).toBe("HTTP.INVALID_INPUT");
|
|
362
|
+
}
|
|
363
|
+
expect((await request.post("/api/notes", { data: { text: "x".repeat(500) } })).status()).toBe(201);
|
|
364
|
+
const missing = await request.post("/api/notes", { data: {} });
|
|
365
|
+
expect(missing.status()).toBe(400);
|
|
366
|
+
expect(await errorCode(missing)).toBe("HTTP.INVALID_INPUT");
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## `apps/template/tests/e2e/ui/account.spec.ts`
|
|
371
|
+
|
|
372
|
+
<!-- example-source: apps/template/tests/e2e/ui/account.spec.ts -->
|
|
373
|
+
```ts
|
|
374
|
+
import { expect, test } from "@playwright/test";
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* What only a browser shows: the home page renders the greeting, and the login button signs in
|
|
378
|
+
* (locally, the authentication mock's default user). Every other case is an API test (../api/).
|
|
379
|
+
*
|
|
380
|
+
* Edge cases, listed before the test (the skill's reference/testing/e2e.md): access is the one that
|
|
381
|
+
* applies here (anonymous sees "Log in", a signed-in user sees "Log out"); boundaries, text, order,
|
|
382
|
+
* repetition and failures are covered by the API and in-process tests.
|
|
383
|
+
*/
|
|
384
|
+
test("the login button signs in, and the page shows who is logged in", async ({ page }) => {
|
|
385
|
+
await page.goto("/");
|
|
386
|
+
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Hello");
|
|
387
|
+
await expect(page.getByText("You are not logged in.")).toBeVisible();
|
|
388
|
+
|
|
389
|
+
await page.getByRole("button", { name: "Log in" }).click();
|
|
390
|
+
await expect(page.getByText(/^Logged in as .+ \(user\)\.$/)).toBeVisible();
|
|
391
|
+
await expect(page.getByRole("button", { name: "Log out" })).toBeVisible();
|
|
392
|
+
|
|
393
|
+
await page.getByRole("button", { name: "Log out" }).click();
|
|
394
|
+
await expect(page.getByText("You are not logged in.")).toBeVisible();
|
|
395
|
+
});
|
|
396
|
+
```
|
|
@@ -32,12 +32,12 @@ Each file has exactly two members:
|
|
|
32
32
|
```json
|
|
33
33
|
{
|
|
34
34
|
"values": { "greeting": "Hello from production" },
|
|
35
|
-
"framework": {
|
|
35
|
+
"framework": {}
|
|
36
36
|
}
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
- `values` holds values for your declared keys; keys with a default may be left out. A key you did not declare, a missing value for a key without a default, or a wrong type fails the build with `CORE.MISCONFIGURED`, naming the file and every bad field.
|
|
40
|
-
- `framework`
|
|
40
|
+
- `framework` is empty (`{}`): the framework sends no email, and who may sign in comes from the owner's Cloudflare account ([current-user.md](current-user.md)). `framework.admins` and `framework.users` fail the build.
|
|
41
41
|
- A production stage reads only its own file and never falls back to `test.json`, so create `config/<target>/<stage>.json` before its first deploy.
|
|
42
42
|
- Never put a secret in these files: there is no place for one.
|
|
43
43
|
|
|
@@ -47,7 +47,7 @@ App code reads configuration only through `c.var.config`. Never read `process.en
|
|
|
47
47
|
|
|
48
48
|
## In tests
|
|
49
49
|
|
|
50
|
-
`createTestApp(app, { config: { greeting: "Hi there" } })` overrides declared values; an undeclared key makes it throw. The reserved key `framework` takes
|
|
50
|
+
`createTestApp(app, { config: { greeting: "Hi there" } })` overrides declared values; an undeclared key makes it throw. The reserved key `framework` takes no `admins` or `users`: they make it throw, as the build does. Test role changes with the fake auth app ([current-user.md](current-user.md)).
|
|
51
51
|
|
|
52
52
|
## Check rules that send you here
|
|
53
53
|
|
|
@@ -59,4 +59,4 @@ App code reads configuration only through `c.var.config`. Never read `process.en
|
|
|
59
59
|
|
|
60
60
|
## Stopping condition
|
|
61
61
|
|
|
62
|
-
Stop reading when each setting is declared with a type, read through `c.var.config.get`, given values in each stage's committed file (or a default), and `
|
|
62
|
+
Stop reading when each setting is declared with a type, read through `c.var.config.get`, given values in each stage's committed file (or a default), and `npm run verify` passes.
|
|
@@ -13,12 +13,12 @@ Store `c.var.user.id` in your rows to record ownership (an `ownerId: text()` col
|
|
|
13
13
|
|
|
14
14
|
## Levels
|
|
15
15
|
|
|
16
|
-
- **
|
|
17
|
-
- **Admins** are
|
|
18
|
-
- The
|
|
16
|
+
- **Only members of the owner's Cloudflare account can sign in.** Level `user` means *any member of that account*, not a person this app trusts; an app that needs finer trust keeps its own records (for example a table of allowed user IDs).
|
|
17
|
+
- **Admins** are members whose account role is one of PAR-413's administrator roles ("Super Administrator - All Privileges", "Administrator"). There is no "user" role, and no list of allowed people.
|
|
18
|
+
- The auth app records the person's role at each sign-in, and the app re-reads that record every 5 minutes (PAR-425). So a role change in the dashboard shows after the person next signs in (to any app on the account), with no deploy; removing a member does not end a session they already have. If the role cannot be confirmed, the person is signed out: a protected route then answers 401.
|
|
19
19
|
- Configuration holds no admins or users: `framework.admins` and `framework.users` are rejected by the build.
|
|
20
20
|
|
|
21
|
-
How someone becomes an admin
|
|
21
|
+
How someone becomes an admin: [owner-tasks.md](owner-tasks.md) §7. In short: the owner gives them one of those roles in the Cloudflare dashboard (Manage Account → Members), and they sign in again; another role takes it away the same way. `/api/auth/me` shows a person's level and user ID.
|
|
22
22
|
|
|
23
23
|
## Protecting routes
|
|
24
24
|
|
|
@@ -37,22 +37,22 @@ From `@lago-morph/prototype-framework/client`: `getCurrentUser()` (the user as t
|
|
|
37
37
|
|
|
38
38
|
A failed login returns to `/?authError=login_failed&requestId=<id>`. Show a message with the request ID; the reason is in the log ([troubleshooting.md](troubleshooting.md)).
|
|
39
39
|
|
|
40
|
-
Sessions last
|
|
40
|
+
Sessions last 12 hours (PAR-250). There is no server-side session list: logging out clears this browser's cookie only.
|
|
41
41
|
|
|
42
42
|
## Which login runs where
|
|
43
43
|
|
|
44
44
|
| Where | Login |
|
|
45
45
|
| --- | --- |
|
|
46
|
-
| Local `dev` |
|
|
47
|
-
| Test stages (`test-*`) |
|
|
48
|
-
| Production stages |
|
|
46
|
+
| Local `dev` | The authentication mock: the login button signs in a test user. |
|
|
47
|
+
| Test stages (`test-*`) | The authentication mock only, for tests (`mockUsers`, `connectTestStage`) |
|
|
48
|
+
| Production stages | Sign-in with Cloudflare through the account's auth app ([owner-tasks.md](owner-tasks.md) §6) |
|
|
49
49
|
|
|
50
50
|
The mock chooses the level directly: it reads no roles.
|
|
51
51
|
|
|
52
52
|
## Testing
|
|
53
53
|
|
|
54
54
|
- Choose the identity per test app: `createTestApp(app, { user: mockUsers.user("alice") })`, or `t.as(mockUsers.admin())` for further requests with another identity (sharing the same storage). `mockUsers.user(id?)` and `mockUsers.admin(id?)` use the ID as given.
|
|
55
|
-
- To test the **real
|
|
55
|
+
- To test the **real sign-in and role changes**, use `createTestApp(app, { authAppLoginProvider: true })`: `await t.loginWithAuthApp({ sub: "cf-1", email: "a@example.com", role: "user" })` signs in through a fake auth app. `t.authApp.setRole("cf-1", "admin")` changes the role the next refresh reads; move `t.clock.advance(…)` past PAR-425 to see it, with no new login. `t.authApp.setUnavailable(true)` makes the refresh fail, which signs the person out. `as()` is not available on such a test app.
|
|
56
56
|
- Assert 401 `AUTH.UNAUTHENTICATED` for anonymous and 403 `AUTH.FORBIDDEN` for a user on admin routes. See [testing.md](testing.md).
|
|
57
57
|
|
|
58
58
|
## Examples
|
|
@@ -62,4 +62,4 @@ The mock chooses the level directly: it reads no roles.
|
|
|
62
62
|
|
|
63
63
|
## Stopping condition
|
|
64
64
|
|
|
65
|
-
Stop reading when every private route has a guard and filters by `c.var.user.id`, admin routes use `requireAdmin()`,
|
|
65
|
+
Stop reading when every private route has a guard and filters by `c.var.user.id`, admin routes use `requireAdmin()`, your tests cover anonymous, user and admin callers, and `npm run verify` passes.
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
# Deploying
|
|
2
2
|
|
|
3
|
-
Read this when you deploy, smoke-test, stop, start or remove a stage on a cloud.
|
|
3
|
+
Read this when you deploy, smoke-test, stop, start or remove a stage on a cloud. Cloudflare setup (the one token), secrets, sign-in and admins are in [owner-tasks.md](owner-tasks.md).
|
|
4
4
|
|
|
5
|
-
Commands run in the app's directory,
|
|
6
|
-
|
|
7
|
-
**Status in this phase:** every command on this page is **not yet verified on Cloudflare in this phase**. The framework's cloud commands have run only against a simulated Cloudflare. Only the Cloudflare API call that `stop` and `start` use (§4) was tried on a real Worker. AWS is a future release: this release deploys to Cloudflare only.
|
|
5
|
+
Commands run in the app's directory, after `npm run cloudflare-setup` has said everything is set up ([owner-tasks.md](owner-tasks.md) §2); each reads the token itself, from the environment or `~/.secrets/cloudflare`. Each `npm run <command> -- …` is the same as `npm exec --no -- prototype-framework <command> …`. Behind an HTTPS proxy, put `NODE_USE_ENV_PROXY=1 ` in front of every cloud command; the command says so if it is needed.
|
|
8
6
|
|
|
9
7
|
## 1. Test stages and production stages
|
|
10
8
|
|
|
11
|
-
- A stage whose name starts with `test-` is a **test stage**. It uses `config/cloudflare/test.json`, has the mock login for tests
|
|
12
|
-
- Every other stage is a **production stage**, for example `prod`. It needs its own committed `config/cloudflare/<stage>.json` ([owner-tasks.md](owner-tasks.md) §5) and the account's
|
|
9
|
+
- A stage whose name starts with `test-` is a **test stage**. It uses `config/cloudflare/test.json`, has only the mock login for tests (no real sign-in), holds no real data, and is removed after use.
|
|
10
|
+
- Every other stage is a **production stage**, for example `prod`. It needs its own committed `config/cloudflare/<stage>.json` ([owner-tasks.md](owner-tasks.md) §5) and the account's auth app (§2 there). It has no mock: members of the owner's Cloudflare account sign in with Cloudflare.
|
|
13
11
|
- A stage name is at most 20 lowercase letters, digits and hyphens. The stage's address is `https://<app>-<stage>.<subdomain>.workers.dev`.
|
|
14
12
|
|
|
15
13
|
## 2. Deploy and smoke-test a test stage
|
|
@@ -44,8 +42,8 @@ npm run smoke -- --target cloudflare --stage "$STAGE"
|
|
|
44
42
|
npm run secrets -- set apiKey --target cloudflare --stage "$STAGE"
|
|
45
43
|
npm run deploy -- --target cloudflare --stage "$STAGE"
|
|
46
44
|
```
|
|
47
|
-
It
|
|
48
|
-
3. Sign
|
|
45
|
+
It registers the stage with the auth app and prints the stage's address.
|
|
46
|
+
3. Sign in on the stage ([owner-tasks.md](owner-tasks.md) §6). Admins are the account's administrators (§7 there); they change without a deploy.
|
|
49
47
|
|
|
50
48
|
To change a production stage later (code or configuration), commit the change and run `deploy` again:
|
|
51
49
|
|
package/skill/reference/files.md
CHANGED
|
@@ -62,4 +62,4 @@ The ticket is the same JSON on every target: `{ url, method?, headers?, expiresA
|
|
|
62
62
|
|
|
63
63
|
## Stopping condition
|
|
64
64
|
|
|
65
|
-
Stop reading when your server builds every file name itself, authorizes from your records, issues tickets with explicit content types, checks uploads with `head` before making them visible, serves content only through `downloadUrl`, and `
|
|
65
|
+
Stop reading when your server builds every file name itself, authorizes from your records, issues tickets with explicit content types, checks uploads with `head` before making them visible, serves content only through `downloadUrl`, and `npm run verify` passes.
|
|
@@ -88,4 +88,4 @@ API responses are never cached (`Cache-Control: no-store`); you need not set cac
|
|
|
88
88
|
|
|
89
89
|
## Stopping condition
|
|
90
90
|
|
|
91
|
-
Stop reading when your route has a guard (or is deliberately open), validates its input, throws only the four codes above, returns a shared type, has a test, and `
|
|
91
|
+
Stop reading when your route has a guard (or is deliberately open), validates its input, throws only the four codes above, returns a shared type, has a test, and `npm run verify` passes. For storage inside the route, read only the reference for that capability.
|
package/skill/reference/kv.md
CHANGED
|
@@ -38,4 +38,4 @@ KV behaves identically on every target; you never see where it is stored.
|
|
|
38
38
|
|
|
39
39
|
## Stopping condition
|
|
40
40
|
|
|
41
|
-
Stop reading when every key your code builds has a fixed prefix plus trusted IDs, values stay under 4,096 bytes, `list` callers pass the cursor back with the same prefix, and `
|
|
41
|
+
Stop reading when every key your code builds has a fixed prefix plus trusted IDs, values stay under 4,096 bytes, `list` callers pass the cursor back with the same prefix, and `npm run verify` passes.
|
|
@@ -32,7 +32,7 @@ Then:
|
|
|
32
32
|
|
|
33
33
|
## Commands
|
|
34
34
|
|
|
35
|
-
Run them in the app directory, as `npm run <command>` or `
|
|
35
|
+
Run them in the app directory, as `npm run <command>` (flags after `--`) or `npm exec --no -- prototype-framework <command>` (never plain `npx`: before `npm install` it would fetch an unrelated package of that name from the registry):
|
|
36
36
|
|
|
37
37
|
| Command | Does |
|
|
38
38
|
| --- | --- |
|
|
@@ -70,4 +70,4 @@ Run them in the app directory, as `npm run <command>` or `npx prototype-framewor
|
|
|
70
70
|
|
|
71
71
|
## Stopping condition
|
|
72
72
|
|
|
73
|
-
Stop reading when `npm run dev` serves your app at `http://localhost:<port>/`, you can log in with the mock, and `
|
|
73
|
+
Stop reading when `npm run dev` serves your app at `http://localhost:<port>/`, you can log in with the mock, and `npm run verify` passes. For deploying to a cloud, read [deploying.md](deploying.md).
|
package/skill/reference/log.md
CHANGED
|
@@ -51,4 +51,4 @@ On a connected test stage (`connectTestStage`) `logs()` throws: entries are in t
|
|
|
51
51
|
|
|
52
52
|
## Stopping condition
|
|
53
53
|
|
|
54
|
-
Stop reading when your code logs what only it knows, without secrets or personal data, does not duplicate the framework's error entries, your error displays show the request ID, and `
|
|
54
|
+
Stop reading when your code logs what only it knows, without secrets or personal data, does not duplicate the framework's error entries, your error displays show the request ID, and `npm run verify` passes.
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
# Owner tasks
|
|
2
2
|
|
|
3
|
-
Read this when the app's owner (or an agent acting for them) must do something outside the code: set up
|
|
3
|
+
Read this when the app's owner (or an agent acting for them) must do something outside the code: set up Cloudflare (one token, one command), set secrets, create a production stage, sign in, make someone an admin, or keep costs down. Give the owner these steps as written. The owner does not read code.
|
|
4
4
|
|
|
5
|
-
Commands run in a terminal, in the app's directory, on macOS or Linux with Node 22.22.0. Replace `<…>` parts
|
|
6
|
-
|
|
7
|
-
**Status in this phase.** The local steps are verified. The Cloudflare steps have run against a simulated Cloudflare, and the email setup once against the real account; treat the rest as **not yet verified on Cloudflare**. If a step fails, follow [troubleshooting.md](troubleshooting.md).
|
|
5
|
+
Commands run in a terminal, in the app's directory, on macOS or Linux with Node 22.22.0. Replace `<…>` parts. Never paste a secret value or a token into a command line, a file in the repository, or a chat.
|
|
8
6
|
|
|
9
7
|
## 1. Before anything else (once per checkout)
|
|
10
8
|
|
|
@@ -16,7 +14,7 @@ npm run build -w packages/framework
|
|
|
16
14
|
cd apps/template
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
An app made with
|
|
17
|
+
An app made with `npm create` is already installed.
|
|
20
18
|
|
|
21
19
|
Set the stage's name in `STAGE` once in each new terminal (`<stage>` is for example `test-abc` or `prod`):
|
|
22
20
|
|
|
@@ -24,36 +22,47 @@ Set the stage's name in `STAGE` once in each new terminal (`<stage>` is for exam
|
|
|
24
22
|
STAGE='<stage>'
|
|
25
23
|
```
|
|
26
24
|
|
|
27
|
-
## 2. Cloudflare
|
|
25
|
+
## 2. Cloudflare: one token, one command
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
2. Copy your **Account ID** from the account's overview page (menu names here are not yet verified).
|
|
31
|
-
3. Create two **account API tokens**: Manage Account → Account API Tokens → Create Token → Create Custom Token. Account resources: this account only; zone resources: the email domain's zone. Copy each token when shown; Cloudflare shows it only once.
|
|
32
|
-
- **Everyday token** `prototype-framework`, for every command but `setup-account`, including `stop`, `start` and the backup a production `remove` writes. Account: Workers Scripts Edit, Workers R2 Storage Edit, D1 Edit, Email Sending Read. Zone: Zone Read, Analytics Read. It cannot create tokens. It needs no Billing permission: if Cloudflare refuses a new database because the account has too many, the message names both plans' limits (put `PROTOTYPE_FRAMEWORK_CLOUDFLARE_PLAN=free` or `=paid` in front of the command to name yours).
|
|
33
|
-
- **Setup token** `prototype-framework-setup`, for `setup-account` only (§3), used rarely. Account: Workers Scripts Edit, Workers R2 Storage Edit, Account API Tokens Edit, Email Sending Edit, Email Routing Addresses Edit. Zone: Email Routing Rules Edit, DNS Edit, Zone Settings Edit, Zone Read. Store it apart from the everyday token.
|
|
27
|
+
Any Cloudflare account works, on the free Workers plan too. No domain is needed. You need one Cloudflare API token and your account ID. The steps, in order:
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
4. In each new terminal, set both values; paste the everyday token when `read` waits, then press Enter (`read` keeps it out of your shell history):
|
|
29
|
+
1. **Run the setup.** It prints no token value and does only what is missing:
|
|
37
30
|
```sh
|
|
38
|
-
|
|
39
|
-
read -rs CLOUDFLARE_API_TOKEN && export CLOUDFLARE_API_TOKEN
|
|
31
|
+
npm run cloudflare-setup
|
|
40
32
|
```
|
|
41
|
-
|
|
42
|
-
```
|
|
43
|
-
|
|
33
|
+
With no token yet, it prints this two-line template, where the lines go, the token's pre-filled link and the next prompt:
|
|
34
|
+
```
|
|
35
|
+
CLOUDFLARE_API_TOKEN=
|
|
36
|
+
CLOUDFLARE_ACCOUNT_ID=
|
|
44
37
|
```
|
|
38
|
+
2. **Make the token** `prototype-framework-cli` from that link, logged in to the dashboard. Its permissions and name are filled in; keep them (more is fine). Pick your account if asked and choose any end date, or none. Then Continue to summary, Create Token, and copy it: Cloudflare shows it only once. A token you already have with more permissions (an account admin token, say) works too. Your **account ID** is on the account's home page in the dashboard.
|
|
39
|
+
3. **Fill in the two lines and put them in one place** (the environment wins when both are set):
|
|
40
|
+
- **On your computer:** the file `~/.secrets/cloudflare` in your home directory, then `chmod 600 ~/.secrets/cloudflare`. Every app, worktree and Claude Code session on the machine shares it. `CLOUDFLARE_SECRETS_FILE` names another path if you want one.
|
|
41
|
+
- **In a Claude Code cloud session:** the environment's settings (environment menu → Edit → Environment variables), the same two lines; then start a new session.
|
|
42
|
+
Never paste the token into the chat or into a file in the repository.
|
|
43
|
+
4. **Run `npm run cloudflare-setup` again** (in Claude Code, say **"continue prototype setup"**). It says where each value came from (the variable or the file, never the value), checks the token's permissions, and sets the account up in one run: the admin app's client and the sign-in client with their secrets in Cloudflare's Secrets Store, the **admin app**, the account's **auth app** (it signs people in to every app) and the shared resources (§3). It ends with "Cloudflare setup is complete" and the next step, a `deploy` of a `test-` stage. Run again, it says everything is already set up.
|
|
45
44
|
|
|
46
|
-
|
|
45
|
+
**The framework never deletes, rolls or changes your token.** It makes and replaces only the tokens it needs for itself (the file-signing token `prototype-r2-signing`). The admin app (its address is printed once) is optional: it has status and cleanup pages.
|
|
47
46
|
|
|
48
|
-
|
|
47
|
+
**When setup stops** (exit status 1), it says what is missing with the link or command to use. Do that, then run it again; it exits 0 only when all is set up:
|
|
49
48
|
|
|
50
|
-
|
|
49
|
+
| It says | Do this |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| no Cloudflare credential, or one of the two settings is not set | steps 2–3 |
|
|
52
|
+
| the token is not accepted or not active | make a token from the link (step 2) and replace `CLOUDFLARE_API_TOKEN` |
|
|
53
|
+
| the token lacks these permissions (named) | add them to the token in the dashboard (Manage account → Account API tokens → the token → Edit), or make a new one from the link |
|
|
54
|
+
| the token cannot read its own permissions | it lacks Account API Tokens Edit: add it, or make a new token from the link |
|
|
55
|
+
| the token cannot see the account `CLOUDFLARE_ACCOUNT_ID` names (accounts listed by name) | set `CLOUDFLARE_ACCOUNT_ID` to the right account's ID, or make the token on that account |
|
|
56
|
+
| the secrets file can be read by other users | `chmod 600 ~/.secrets/cloudflare` (a warning only) |
|
|
57
|
+
| `.env.cloudflare` or `CLOUDFLARE_BOOTSTRAP_TOKEN` is no longer used | move the token and account ID to `~/.secrets/cloudflare` (or the environment), then delete the old file or variable |
|
|
58
|
+
| a host is blocked | allow `api.cloudflare.com`, `*.r2.cloudflarestorage.com` and `*.workers.dev` in the environment's network access settings |
|
|
59
|
+
| an old `.env.cloudflare` is not ignored, or is tracked | setup adds the `.gitignore` line unless it holds `# keep-env-cloudflare-tracked`. Tracked: delete the tokens it held, `git rm --cached .env.cloudflare`, commit |
|
|
51
60
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
The git warnings never stop a command. Setup also never touches a token whose ID is in `PROTOTYPE_FRAMEWORK_PROTECTED_TOKEN_IDS` (comma-separated).
|
|
62
|
+
|
|
63
|
+
## 3. What the account setup creates
|
|
55
64
|
|
|
56
|
-
|
|
65
|
+
`cloudflare-setup` creates, once per account: the admin app and the sign-in clients, the **auth app** with its database, the log bucket `prototype-logs`, the deploy-state bucket `sst-state`, the backup bucket `prototype-backups` (a removed production stage's data, kept 30 days), and the file-signing credential every stage uses, kept in Secrets Store. It records what it did in `framework/account.json` in `sst-state`. `deploy` never creates these resources; if they are missing, it says so. When setup replaces the file-signing credential, redeploy every deployed stage (`npm run deploy -- --target cloudflare --stage <stage>`): until then its file uploads and downloads fail. The admin app's Account setup page does the same and is optional.
|
|
57
66
|
|
|
58
67
|
## 4. Secrets
|
|
59
68
|
|
|
@@ -79,7 +88,7 @@ mkdir -p config/cloudflare
|
|
|
79
88
|
cp ../../skill/templates/production-stage.json "config/cloudflare/$STAGE.json"
|
|
80
89
|
```
|
|
81
90
|
|
|
82
|
-
|
|
91
|
+
`"values"` may set the app's configuration keys; ask the developer which. Then check, commit and deploy, setting the secrets the first deploy names ([deploying.md](deploying.md)):
|
|
83
92
|
|
|
84
93
|
```sh
|
|
85
94
|
npm run build -- --target cloudflare --stage "$STAGE"
|
|
@@ -87,64 +96,41 @@ git add "config/cloudflare/$STAGE.json" && git commit -m "Add the values file of
|
|
|
87
96
|
npm run deploy -- --target cloudflare --stage "$STAGE"
|
|
88
97
|
```
|
|
89
98
|
|
|
90
|
-
The deploy prints the
|
|
99
|
+
The deploy registers the stage with the auth app and prints its address. Without the account's auth app it names it and stops before creating anything: run `npm run cloudflare-setup` (§2) first.
|
|
91
100
|
|
|
92
|
-
## 6. Signing
|
|
101
|
+
## 6. Signing in
|
|
93
102
|
|
|
94
|
-
**
|
|
103
|
+
**Only members of your Cloudflare account can sign in**, with their Cloudflare login. Anyone else is refused. No list in the app decides who may log in.
|
|
95
104
|
|
|
96
|
-
1. Open the stage's
|
|
97
|
-
2.
|
|
98
|
-
3.
|
|
105
|
+
1. Open the stage's address and press the login button (or open `<stage address>/api/auth/login`).
|
|
106
|
+
2. Sign in on Cloudflare's page, and allow the app to see your user details and memberships.
|
|
107
|
+
3. You are back on the app. `<stage address>/api/auth/me` shows `"level":"user"` or `"level":"admin"`.
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
- **Locally** the mail is not sent; it is written to `.mail/`. Read the latest message to an address:
|
|
103
|
-
```sh
|
|
104
|
-
npm run mail -- last --to '<address>'
|
|
105
|
-
```
|
|
106
|
-
- **On Cloudflare**, sign up with a test address `sink+<tag>@t.<domain>` (`<tag>`: lowercase letters, digits, hyphens), then read it:
|
|
107
|
-
```sh
|
|
108
|
-
npm run mail -- last --to 'sink+<tag>@t.<domain>' --target cloudflare
|
|
109
|
-
```
|
|
110
|
-
- Without a browser, post the link's token (the value after `token=`) instead of pressing the button:
|
|
111
|
-
```sh
|
|
112
|
-
curl -sS -X POST '<stage address>/api/auth/password/verify' -H 'Origin: <stage address>' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"token":"<token>"}'
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
A forgotten password: "Forgot your password?" on the sign-in page mails a reset link, valid for one hour. "Send a new confirmation link" there mails a new verification link.
|
|
109
|
+
To let someone sign in, invite them to the account (Manage Account → Members). A read-only role works, for example "HTTP Applications Read" (plan-v0.2.0 §2.1a). Locally and on `test-` stages there is no real sign-in: the login button signs in a test user.
|
|
116
110
|
|
|
117
111
|
## 7. Admins
|
|
118
112
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
- **Grant** (on a production stage, repeat its name with `--confirm-production`):
|
|
122
|
-
```sh
|
|
123
|
-
npm run admins -- grant '<address>' --target cloudflare --stage "$STAGE" --confirm-production "$STAGE"
|
|
124
|
-
```
|
|
125
|
-
- **Revoke:** the same with `revoke`. The person is a user again.
|
|
126
|
-
- **List** the stage's admins:
|
|
127
|
-
```sh
|
|
128
|
-
npm run admins -- list --target cloudflare --stage "$STAGE" --confirm-production "$STAGE"
|
|
129
|
-
```
|
|
130
|
-
- On a test stage, leave out `--confirm-production`. Locally, use `--target local` (no `--stage`); it works whether or not `npm run dev` runs.
|
|
113
|
+
**A member whose account role is "Super Administrator - All Privileges" or "Administrator" (PAR-413) is the app's admin**; every other member is a user. To make someone an admin, give them one of those roles in the dashboard (Manage Account → Members); to make them a user again, give them another role. No file edit, no commit and no deploy.
|
|
131
114
|
|
|
132
|
-
|
|
115
|
+
The apps learn a role change when the person **next signs in**, to any app on the account; their other open sessions pick it up within 5 minutes (PAR-425). Until then they keep their old level: ask them to log out and in again. Removing someone from the account stops new sign-ins, but a session they already have lasts until it expires (12 hours, PAR-250) or they log out.
|
|
133
116
|
|
|
134
|
-
## 8. Checking the real
|
|
117
|
+
## 8. Checking the real sign-in (optional)
|
|
135
118
|
|
|
136
|
-
`npm run check-login -- --target cloudflare --stage "$STAGE"`
|
|
119
|
+
`npm run check-login -- --target cloudflare --stage "$STAGE"` checks, on a production stage and with no sign-in, that the stage sends people to the auth app with its own address. It needs no input. The real sign-in itself needs a person: sign in once (§6).
|
|
137
120
|
|
|
138
121
|
## 9. Cost
|
|
139
122
|
|
|
140
|
-
- Stopped or idle stages cost nothing for compute, but storage remains: the stage's D1 database and R2 file bucket, the shared `prototype-logs` bucket,
|
|
141
|
-
-
|
|
142
|
-
- Keep test stages small: a few rows of data, one test stage at a time, and `remove` it right after use. An account holds at most **10 D1 databases**, and a deploy that changes the schema briefly uses two per stage.
|
|
123
|
+
- Stopped or idle stages cost nothing for compute, but storage remains: the stage's D1 database and R2 file bucket, the shared `prototype-logs` bucket, the `sst-state` bucket, and the auth app's database. Log entries are never deleted automatically.
|
|
124
|
+
- Keep test stages small: a few rows of data, one test stage at a time, and `remove` it right after use. An account holds at most **10 D1 databases** (the auth app uses one), and a deploy that changes the schema briefly uses two per stage.
|
|
143
125
|
- See [deploying.md](deploying.md) for deploy, stop/start and remove.
|
|
144
126
|
|
|
127
|
+
## 10. Coming from 0.1.x
|
|
128
|
+
|
|
129
|
+
`npm run upgrade` to 0.2.0 says Cloudflare setup needs re-running. Once per account, do §2: put one token with the listed permissions (your old everyday token lacks some) and the account ID in `~/.secrets/cloudflare` or the environment, delete `.env.cloudflare`, and run `npm run cloudflare-setup`. Password accounts and sessions are gone, so everyone signs in again with Cloudflare; the old sign-in and test-mail tables are dropped on the next `dev` or `deploy`, and the app's own data stays. Remove `framework.email` from values files if you like: it is no longer used. The admin app's **Clean up v0.1.x email** page optionally removes the old email resources (sending domain, routing rules, test mailbox); nothing is removed automatically.
|
|
130
|
+
|
|
145
131
|
## Examples
|
|
146
132
|
|
|
147
|
-
- [examples/configuration.md](../examples/configuration.md): the template's committed values files (`config/<target>/<stage>.json`), which hold the app's values and
|
|
133
|
+
- [examples/configuration.md](../examples/configuration.md): the template's committed values files (`config/<target>/<stage>.json`), which hold the app's values and no admins.
|
|
148
134
|
|
|
149
135
|
## Stopping condition
|
|
150
136
|
|
|
@@ -14,8 +14,8 @@ Values are not cached across requests (a value is reused only within one request
|
|
|
14
14
|
|
|
15
15
|
A secret's value is read from standard input, never from a command-line argument or a file in the repository.
|
|
16
16
|
|
|
17
|
-
- Local development: `printf '%s' 'the-value' |
|
|
18
|
-
- A cloud stage: `
|
|
17
|
+
- Local development: `printf '%s' 'the-value' | npm run secrets -- set apiKey --target local --stage local`. It writes the git-ignored `.dev.vars` file and prints the name and location, never the value. `dev` also generates the framework's own local keys there on first start.
|
|
18
|
+
- A cloud stage: `npm run secrets -- set apiKey --target cloudflare --stage <stage>`, then type or paste the value and end input. On Cloudflare the stage's Worker must exist, so on a stage's first deploy `deploy` stops, names each unset secret, and asks you to set them and deploy again.
|
|
19
19
|
- The owner's copy-and-paste steps for this are in [owner-tasks.md](owner-tasks.md).
|
|
20
20
|
|
|
21
21
|
A deploy's health check fails when any declared secret has no value, so a secret the app declares must be set on every stage it is deployed to.
|
|
@@ -39,4 +39,4 @@ For a signed token such as a share link, copy the template's `src/server/signing
|
|
|
39
39
|
|
|
40
40
|
## Stopping condition
|
|
41
41
|
|
|
42
|
-
Stop reading when every secret is declared in `secrets`, read only through `c.var.secrets.get`, never returned or logged, set on each stage with `secrets set`, given a value in tests, and `
|
|
42
|
+
Stop reading when every secret is declared in `secrets`, read only through `c.var.secrets.get`, never returned or logged, set on each stage with `secrets set`, given a value in tests, and `npm run verify` passes.
|
|
@@ -65,9 +65,9 @@ Build every write on the step's own `w`, never on `c.var.sql`; do not `await` in
|
|
|
65
65
|
## Migrations
|
|
66
66
|
|
|
67
67
|
1. Change the tables in `src/server/schema.ts`.
|
|
68
|
-
2. Run `npm run generate-migration` (or `
|
|
68
|
+
2. Run `npm run generate-migration` (or `npm run generate-migration -- --name <slug>`). It writes a pair of files, `migrations/sqlite/<nnnn>_<slug>.sql` and `migrations/postgres/<nnnn>_<slug>.sql`, plus snapshots under `meta/`, and records their checksums in `migrations/checksums.json`.
|
|
69
69
|
3. Commit all of them. Never edit a generated file. A migration that was never deployed to any cloud stage may be deleted (both files, their `meta/` snapshots) and generated again.
|
|
70
|
-
4. `
|
|
70
|
+
4. `npm run verify` checks both histories give the same schema and that only supported changes are used.
|
|
71
71
|
|
|
72
72
|
Supported changes: add a table; add a column that is nullable or has a static default; drop a column; drop a table; add or drop an index or a unique constraint.
|
|
73
73
|
|
|
@@ -101,4 +101,4 @@ Locally, `dev` and `createTestApp` apply pending migrations. On a cloud, `deploy
|
|
|
101
101
|
|
|
102
102
|
## Stopping condition
|
|
103
103
|
|
|
104
|
-
Stop reading when your tables use only the helpers above, your queries use only the allowed column, a migration pair has been generated and committed for every schema change, and `
|
|
104
|
+
Stop reading when your tables use only the helpers above, your queries use only the allowed column, a migration pair has been generated and committed for every schema change, and `npm run verify` passes on both dialects.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# End-to-end tests
|
|
2
|
+
|
|
3
|
+
Read this when you add or change a flow (something a person or an operator does from start to finish), after a framework upgrade, or when an e2e test fails or must change.
|
|
4
|
+
|
|
5
|
+
## What e2e covers
|
|
6
|
+
|
|
7
|
+
Every user-facing flow, on its **happy path**, its **expected errors** and its **edge cases**. A flow is not done until all three are covered end to end. When a flow changes, change its e2e tests in the same work; after `npm run upgrade`, run them and fix what the release changed. A change to an e2e test that passed before is listed with its reason like any other ([test-changes.md](test-changes.md)).
|
|
8
|
+
|
|
9
|
+
## API first, a few UI tests
|
|
10
|
+
|
|
11
|
+
The template ships two Playwright projects (`playwright.config.ts`), against a local `dev` server that Playwright starts and stops:
|
|
12
|
+
|
|
13
|
+
- **`e2e-api`** (`tests/e2e/api/*.spec.ts`): most e2e tests. They drive the app's HTTP API with Playwright's `request` fixture, which needs no browser. `npm run verify` runs this project after the unit tests; `npm run test:e2e:api` runs it alone.
|
|
14
|
+
- **`e2e-ui`** (`tests/e2e/ui/*.spec.ts`): only what a browser alone shows: a page renders the data, the login button signs in, a file uploads or downloads from the page, a cookie is set. At most a few per flow; every other case of the flow is an API test. `npm run test:e2e:ui` runs it; only this project needs Chromium (`npm exec --no -- playwright install chromium` once, when the user agrees). If Chromium cannot be installed, report the UI run as `not-run` with the reason (hard rule 8) and do not declare the page done.
|
|
15
|
+
|
|
16
|
+
The config hides the server's own log. If the server does not start (Playwright times out waiting for it), run `npm run dev` to see why.
|
|
17
|
+
|
|
18
|
+
Write a spec from the flow, not from the code:
|
|
19
|
+
|
|
20
|
+
- Requests go to paths (`/api/notes`); the config sets the base URL and the `Origin` header the framework's forgery check needs.
|
|
21
|
+
- Sign in as a test identity with the authentication mock, which every local server has: `` await request.post("/api/auth/mock-login", { data: { level: "user", id: `e2e-${crypto.randomUUID()}` } }) ``. Use a fresh ID per test: local data is kept between runs, so never assert totals.
|
|
22
|
+
- Check status codes and `(await res.json()).error.code`, never messages.
|
|
23
|
+
- In UI specs, find elements by role and visible text (`page.getByRole("button", { name: "Log in" })`).
|
|
24
|
+
- Never wait in real time, and never test expiry here: use fake timers in-process ([../testing.md](../testing.md)).
|
|
25
|
+
- `playwright.config.ts` is part of the gate: a change to it that could run fewer tests or pass more easily (projects, `testIgnore`, `retries`, timeouts) is listed like a change to a once-green test ([test-changes.md](test-changes.md)).
|
|
26
|
+
|
|
27
|
+
## Edge cases
|
|
28
|
+
|
|
29
|
+
Before writing a flow's tests, list its edge cases in the spec's header comment, and cover each that applies; for each prompt that does not apply, say why in a few words:
|
|
30
|
+
|
|
31
|
+
- **Boundaries:** empty, one, exactly at a documented limit, one past it. Locally only; on a cloud, keep data small.
|
|
32
|
+
- **Text:** Unicode, case and normalisation; `..`, `/` and percent-encoding in names; one name a prefix of another.
|
|
33
|
+
- **Order and time:** sort order and paging; expiry at and just past the limit (fake `Date`, in-process).
|
|
34
|
+
- **Access:** anonymous, user, admin, and another user's data.
|
|
35
|
+
- **Repetition:** repeated and concurrent requests; reuse of one-time links or tickets.
|
|
36
|
+
- **Failures:** an unavailable capability (in-process with `setUnavailable`); local versus cloud differences.
|
|
37
|
+
|
|
38
|
+
## Examples
|
|
39
|
+
|
|
40
|
+
- [examples/tests.md](../../examples/tests.md): the template's tests, including its e2e specs.
|
|
41
|
+
|
|
42
|
+
## Stopping condition
|
|
43
|
+
|
|
44
|
+
Stop reading when every flow you added or changed has API tests for its happy path, expected errors and listed edge cases, at most a few UI tests for what only a browser shows, and `npm run verify` (and `npm run test:e2e:ui` if you changed a page) passes.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Diagnosing a failure
|
|
2
|
+
|
|
3
|
+
Read this when a test, `verify`, an e2e run, a build, a deploy or a smoke test fails, other than a test that is red on purpose during red/green, or when you are asked to diagnose, review or fix such a failure.
|
|
4
|
+
|
|
5
|
+
"Probably", "flaky" and "timing" are not causes without evidence. Never change a test or the code to make the failure go away before its cause is reviewed.
|
|
6
|
+
|
|
7
|
+
## Who does what
|
|
8
|
+
|
|
9
|
+
Each role is its own subagent. None of them wrote the code that failed, and the reviewer never reviews a cause it found itself. The main session briefs them with file paths, not file contents, and they answer with short summaries; the details go in the record.
|
|
10
|
+
|
|
11
|
+
## The record
|
|
12
|
+
|
|
13
|
+
One record per failure, in the app's repository: `test-records/root-causes/<yyyy-mm-dd>-<slug>.md`. Its sections, in this order:
|
|
14
|
+
|
|
15
|
+
1. **Symptom**: the exact command and output.
|
|
16
|
+
2. **Evidence**: logs (`.logs/`, or the stage's log entry for the `requestId`, [troubleshooting.md](../troubleshooting.md)), requests and responses, state, variations of the test that confirm or rule out each hypothesis, and a minimal reproduction.
|
|
17
|
+
3. **Causal chain**, with the evidence for each link.
|
|
18
|
+
4. **Alternatives ruled out.**
|
|
19
|
+
5. **Proposed fix.**
|
|
20
|
+
6. **Verification plan.**
|
|
21
|
+
7. **Review**, which the reviewer writes. Until then the record says `Status: awaiting review`.
|
|
22
|
+
|
|
23
|
+
An unproven link means more evidence, never a guess.
|
|
24
|
+
|
|
25
|
+
## Steps
|
|
26
|
+
|
|
27
|
+
1. **Diagnose.** The diagnoser reproduces the failure, gathers the evidence and writes the record.
|
|
28
|
+
2. **Review.** The reviewer checks that the evidence supports every link, that the alternatives are ruled out, and that the fix addresses the cause, not the symptom. Its verdict is `accepted` or `rejected`, with reasons. A rejected record goes back to step 1.
|
|
29
|
+
3. **Fix, red then green.** A fixer adds the reproduction test, sees it fail, then fixes the code as the accepted record says, and sees it pass. Where the same defect is plainly in other code it is changing, it fixes and tests that too.
|
|
30
|
+
4. **Re-check.** A subagent that changed nothing re-runs the affected tests and `npm run verify`, and checks that no test was skipped or weakened. Its verdict is the only pass.
|
|
31
|
+
|
|
32
|
+
## Limits
|
|
33
|
+
|
|
34
|
+
- At most **3 attempts** (steps 1–4) per failure.
|
|
35
|
+
- Still open after 3 attempts: stop and tell the user, in plain words, what fails, what was tried, and the record's path.
|
|
36
|
+
- A suspected framework defect goes to `FRAMEWORK-ISSUE.md` (`SKILL.md` hard rules 6 and 7), with the record as its evidence.
|
|
37
|
+
|
|
38
|
+
## Examples
|
|
39
|
+
|
|
40
|
+
- [examples/tests.md](../../examples/tests.md): the tests a reproduction test is added beside.
|
|
41
|
+
|
|
42
|
+
## Stopping condition
|
|
43
|
+
|
|
44
|
+
Stop reading when the failure has an accepted record and its fix passed the re-check, or when it stopped at the limits and you told the user.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Fuzzing a route input
|
|
2
|
+
|
|
3
|
+
Read this when a route takes input at a trust boundary (a name, a cursor, a return path, a code or token, a request body) and its example tests keep missing cases, or the same route has failed more than once.
|
|
4
|
+
|
|
5
|
+
## Where it is worth it
|
|
6
|
+
|
|
7
|
+
Fuzzing is **local, limited and targeted**. Use it only where it finds what example tests would miss:
|
|
8
|
+
|
|
9
|
+
- a parser, validator or encoder at a trust boundary;
|
|
10
|
+
- especially a route with repeated failures (see its records under `test-records/root-causes/`).
|
|
11
|
+
|
|
12
|
+
Having a zod schema makes a route eligible, not required. Never fuzz every route, never run long or exhaustive runs, and never fuzz a deployed stage. Fuzzing never replaces the route's example tests ([../testing.md](../testing.md)).
|
|
13
|
+
|
|
14
|
+
## The template's helper
|
|
15
|
+
|
|
16
|
+
`tests/support/fuzz.ts` drives a route from its zod schema with the `fast-check` package (pinned, hard rule 5): it sends generated bodies, both valid and invalid for the schema, and checks that the route never answers a server error, answers 400 `HTTP.INVALID_INPUT` when the schema rejects the body, and never answers it when the schema accepts the body (the route's own rules may still refuse it, for example with 404 or 409). Each use states its run count and fixed seed, with a one-line reason, so a run is small and repeatable and adds only seconds to `verify`. See `tests/fuzz.test.ts` for the template's one use.
|
|
17
|
+
|
|
18
|
+
## A finding
|
|
19
|
+
|
|
20
|
+
A failing run prints the seed and the smallest body that fails. Treat it as any failure ([failures.md](failures.md)), and add that body as a plain example test before the fix.
|
|
21
|
+
|
|
22
|
+
## Examples
|
|
23
|
+
|
|
24
|
+
- [examples/tests.md](../../examples/tests.md): the in-process tests the helper runs beside.
|
|
25
|
+
|
|
26
|
+
## Stopping condition
|
|
27
|
+
|
|
28
|
+
Stop reading when you have decided the route does not qualify, or its fuzz test states its run count, seed and reason, passes in `npm run verify`, and any finding is a plain regression test.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Test impact and changes to passing tests
|
|
2
|
+
|
|
3
|
+
Read this when a change needs new, changed or removed tests, or you are about to change, remove, skip or loosen a test that has passed before.
|
|
4
|
+
|
|
5
|
+
## Test impact of every change
|
|
6
|
+
|
|
7
|
+
Before you change code, write down its test impact and then make it, in the same work, without being asked:
|
|
8
|
+
|
|
9
|
+
- the unit and in-process tests to add, change or remove;
|
|
10
|
+
- the e2e tests to add, change or remove ([e2e.md](e2e.md));
|
|
11
|
+
- any fuzz target affected ([fuzzing.md](fuzzing.md)).
|
|
12
|
+
|
|
13
|
+
When you report to the user, list the test impact and each change to an existing test with its reason code (below).
|
|
14
|
+
|
|
15
|
+
## Changes to a once-green test
|
|
16
|
+
|
|
17
|
+
A test is **once-green** when it passed in this session or is on the app's main branch. This covers every layer: unit, in-process, e2e, smoke and fuzz.
|
|
18
|
+
|
|
19
|
+
- **What to list:** any change to its expected results or assertions, its removal, a skip (`.skip`, `.todo`, a condition that stops it running), a wider tolerance or timeout, or a renamed or merged test that checks less. List each one, grouped by reason code (below), when you report to the user.
|
|
20
|
+
- **Never weaken a test to pass.** A test you think is wrong is a failure to diagnose ([failures.md](failures.md)); the reason code is then `test-was-wrong`, citing the record.
|
|
21
|
+
|
|
22
|
+
## Reason codes
|
|
23
|
+
|
|
24
|
+
Group the changes and give each group one code:
|
|
25
|
+
|
|
26
|
+
| Code | When | Cite |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| `code-removed` | the code or feature it tests is gone | the user's request or the change that removed it |
|
|
29
|
+
| `behaviour-changed` | the app's intended behaviour changed | the requirement or the user's request |
|
|
30
|
+
| `test-was-wrong` | the test asserted the wrong thing | its root-cause record |
|
|
31
|
+
| `strengthened` | it now checks more | — |
|
|
32
|
+
| `refactor-only` | same checks, new shape | — |
|
|
33
|
+
|
|
34
|
+
A test weakened while the code it covers stayed the same needs a clear reason in the list; if you can't give one, it is a failure to diagnose ([failures.md](failures.md)).
|
|
35
|
+
|
|
36
|
+
## Examples
|
|
37
|
+
|
|
38
|
+
- [examples/tests.md](../../examples/tests.md): the template's tests, which a change set extends.
|
|
39
|
+
|
|
40
|
+
## Stopping condition
|
|
41
|
+
|
|
42
|
+
Stop reading when the change's test impact is written down and done, and every change to a once-green test is listed with its reason.
|
|
@@ -1,50 +1,61 @@
|
|
|
1
1
|
# Testing
|
|
2
2
|
|
|
3
|
-
Read this when you write or fix app tests, write the smoke test, test time-dependent behaviour, or need to know what `verify
|
|
3
|
+
Read this when you write or fix app tests, write the smoke test or the e2e tests, test time-dependent behaviour, or need to know what `verify`, the e2e projects and `smoke` run.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## How you work on tests
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
These hold for every change, without asking the user technical questions. Explain results to the user in plain words.
|
|
8
|
+
|
|
9
|
+
- **Red, green, refactor.** For app logic, write one failing test first, run it and see it fail for the expected reason, then write the code, then tidy up. Every fix starts with a test that reproduces the failure and fails.
|
|
10
|
+
- **State each change's test impact** and make the test changes in the same work: the unit, integration and e2e tests to add, change or remove, and any fuzz target ([testing/test-changes.md](testing/test-changes.md)).
|
|
11
|
+
- **Keep e2e current.** A flow is not done until its happy path, its expected errors and its edge cases are covered end to end. When a flow changes, and after a framework upgrade, update its e2e tests ([testing/e2e.md](testing/e2e.md)).
|
|
12
|
+
- **A failure is diagnosed before anything changes.** Any failure other than a test that is red on purpose gets an evidence-based root cause from a subagent that did not write the code, an independent review, and only then a red-green fix ([testing/failures.md](testing/failures.md)). "Flaky" or "timing" is not a cause without evidence.
|
|
13
|
+
- **Never skip, delete or weaken a test to get a pass.** Any change to a test that passed once (its expected results, its removal, a skip, a wider tolerance or timeout) is listed with its reason when you report ([testing/test-changes.md](testing/test-changes.md)).
|
|
14
|
+
- **Orchestrate.** The main session briefs subagents, in parallel where they touch different files, and keeps its own context small, unless the user says otherwise. Roles that must be separate: the code's author never diagnoses or reviews its own failure.
|
|
15
|
+
- **CI is the user's.** Installing or upgrading the framework never creates or changes the app's CI. You may suggest a CI change to the user, who decides. Apps have no commit hooks: run the checks yourself.
|
|
16
|
+
|
|
17
|
+
## The layers
|
|
18
|
+
|
|
19
|
+
| Layer | What | Where | Runs with |
|
|
20
|
+
| --- | --- | --- | --- |
|
|
21
|
+
| Unit and integration | App logic and routes, in process, with real local storage on both SQL dialects | `tests/**/*.test.ts` | `npm run verify` |
|
|
22
|
+
| E2E, API | Each flow through the running app's HTTP API | `tests/e2e/api/*.spec.ts` | `npm run verify` (project `e2e-api`, no browser) |
|
|
23
|
+
| E2E, UI | What only a browser shows: page rendering, the login button, file transfer in the page, cookies; a few per flow | `tests/e2e/ui/*.spec.ts` | `npm run test:e2e:ui` (project `e2e-ui`, needs Chromium) |
|
|
24
|
+
| Fuzz | A route input at a trust boundary, from its zod schema, only where [testing/fuzzing.md](testing/fuzzing.md) says | `tests/**/*.test.ts` | `npm run verify` |
|
|
25
|
+
| Smoke | A short check on a deployed `test-` stage | `tests/smoke/**/*.test.ts` | `npm run smoke` |
|
|
26
|
+
|
|
27
|
+
- `npm run verify` runs every `tests/**/*.test.ts` file (PAR-116) with Vitest twice, once on SQLite and once on Postgres, each in its own process, with 60 s test and hook timeouts, then the `e2e-api` project against a local `dev` server it starts. It fails if any test fails or if no test passed. Tests need no network.
|
|
28
|
+
- `npm run smoke -- --target <t> --stage <s>` runs only `tests/smoke/**/*.test.ts`, against the running `dev` server (`--target local`) or against a deployed **test** stage. It refuses to run when tracked files differ from the last commit, writes an evidence record to `validation/<target>/<runId>.json` at the top of the git repository that holds the app (commit it with the run's commit), and prints the stage's log location for the day.
|
|
9
29
|
- An app is not done on a cloud until its smoke test has passed on a deployed stage there.
|
|
10
|
-
- Your tests and your smoke test reach the app only through its API; behaviour that exists only in the page (how text renders, the clipboard, an image displaying) is checked by the framework's browser tests, not the app's, so keep page text to `textContent` (`no-raw-html`), test the API side, and do not add a browser driver or raise this as a framework issue.
|
|
11
30
|
|
|
12
31
|
## In-process tests
|
|
13
32
|
|
|
14
33
|
Import from `@lago-morph/prototype-framework/testing` (only in `tests/**`):
|
|
15
34
|
|
|
16
|
-
- `await createTestApp(app, options?)` starts your app in the test process with fresh, empty storage and migrations applied. Options: `user` (identity for every request, default anonymous), `config` (override declared values
|
|
35
|
+
- `await createTestApp(app, options?)` starts your app in the test process with fresh, empty storage and migrations applied. Options: `user` (identity for every request, default anonymous), `config` (override declared values), `secrets` (values), `unavailable` (e.g. `["sql"]`: every operation of that capability fails with its `*.UNAVAILABLE` code), `authAppLoginProvider` (see [current-user.md](current-user.md)). `t.setUnavailable([...])` replaces the unavailable set from the next request on (`[]` makes everything available again; stored data is kept), so a test can store data, make `files` unavailable, run the request that cleans up, and check its status and the `warn` entry in `t.logs()`. Not available against a deployed stage.
|
|
17
36
|
- `await t.request("/api/notes", init?)` sends a request and returns a `Response`. Include `/api` in the path. For JSON bodies set `headers: { "content-type": "application/json" }` and `body: JSON.stringify(…)`. State-changing requests get the right `Origin` automatically.
|
|
18
|
-
- `t.as(mockUsers.user("alice"))` returns a test app that sends requests as Alice and shares storage with `t`; use two of them to test that one user cannot see another's data. `mockUsers.admin(id?)` and `mockUsers.anonymous` likewise.
|
|
19
|
-
- `t.logs()` returns the log entries written so far ([log.md](log.md)).
|
|
20
|
-
- `await t.dispose()` after each test (an `afterEach` that disposes every test app the test opened, as in the examples).
|
|
37
|
+
- `t.as(mockUsers.user("alice"))` returns a test app that sends requests as Alice and shares storage with `t`; use two of them to test that one user cannot see another's data. `mockUsers.admin(id?)` and `mockUsers.anonymous` likewise. `t` and every test app made from it share one app: dispose them only at the end of the test.
|
|
38
|
+
- `t.logs()` returns the log entries written so far ([log.md](log.md)). `await t.dispose()` after each test.
|
|
21
39
|
|
|
22
|
-
What to test for each route: the success case with its body; anonymous (401 `AUTH.UNAUTHENTICATED`) and, for admin routes, a plain user (403 `AUTH.FORBIDDEN`); invalid input (400 `HTTP.INVALID_INPUT`); another user's data (not visible, usually 404 `HTTP.NOT_FOUND` or an empty list); and the error codes your route throws. Check error codes through `(await res.json()).error.code`, never through messages.
|
|
23
|
-
|
|
24
|
-
Type response bodies with the shared types: `const body: NoteList = await res.json();`.
|
|
40
|
+
What to test for each route: the success case with its body; anonymous (401 `AUTH.UNAUTHENTICATED`) and, for admin routes, a plain user (403 `AUTH.FORBIDDEN`); invalid input (400 `HTTP.INVALID_INPUT`); another user's data (not visible, usually 404 `HTTP.NOT_FOUND` or an empty list); and the error codes your route throws. Check error codes through `(await res.json()).error.code`, never through messages. Type response bodies with the shared types: `const body: NoteList = await res.json();`.
|
|
25
41
|
|
|
26
42
|
## Time-dependent behaviour
|
|
27
43
|
|
|
28
|
-
Expiry and other time checks
|
|
44
|
+
Expiry and other time checks are tested in-process with Vitest's fake timers: `vi.useFakeTimers({ toFake: ["Date"] })`, then `vi.setSystemTime(…)` or `vi.advanceTimersByTime(…)`, and `vi.useRealTimers()` afterwards. Fake only `Date`. Never test expiry in the smoke or e2e tests, and never wait in real time.
|
|
29
45
|
|
|
30
46
|
## The smoke test
|
|
31
47
|
|
|
32
|
-
One file under `tests/smoke/` runs both ways:
|
|
33
|
-
|
|
34
|
-
- Under `verify`, it is an ordinary in-process test (`createTestApp`).
|
|
35
|
-
- Under `smoke`, `inject("prototypeFrameworkSmoke")` is `true` and it uses `connectTestStage()`, which connects to the stage `smoke` targets and logs in through the authentication mock.
|
|
48
|
+
One file under `tests/smoke/` runs both ways: under `verify` it is an ordinary in-process test (`createTestApp`); under `smoke`, `inject("prototypeFrameworkSmoke")` is `true` and it uses `connectTestStage()`, which connects to the stage `smoke` targets and logs in through the authentication mock.
|
|
36
49
|
|
|
37
50
|
Keep it short: the health route (`GET /api/__framework/health` answers 204), one open route, and one write-then-read per capability your app depends on. Use a fresh mock user per run (`` mockUsers.user(`smoke-${crypto.randomUUID()}`) ``) because stage data is kept between runs; do not assert totals that earlier runs would change. `logs()` is not available against a stage. Copy the template's file and change the routes ([examples/tests.md](../examples/tests.md)).
|
|
38
51
|
|
|
39
|
-
`t.request` sends only to the app
|
|
40
|
-
|
|
41
|
-
To run it on a cloud, deploy a test stage first, for example `npx prototype-framework deploy --target cloudflare --stage test-1`, then `npx prototype-framework smoke --target cloudflare --stage test-1`, and remove the stage afterwards ([deploying.md](deploying.md)). Test stages use `config/<target>/test.json` and the authentication mock and hold no real data.
|
|
52
|
+
`t.request` sends only to the app; a URL on any other origin is refused. A ticket's `url` is a path on the app locally (send it with `t.request`) and an absolute signed storage URL on a cloud (send it with plain `fetch`, no cookie): choose with `/^\/(?![\/\\])/.test(ticket.url)`. Behind a proxy, `smoke` needs `NODE_USE_ENV_PROXY=1` for that `fetch`. To run it on a cloud, deploy a test stage, run `npm run smoke -- --target cloudflare --stage test-1`, then remove the stage ([deploying.md](deploying.md)).
|
|
42
53
|
|
|
43
54
|
## Failures
|
|
44
55
|
|
|
45
56
|
- A test that passes on SQLite but fails on Postgres (or the reverse) uses something the portable subset does not promise: check ordering (end with the `id` column), NULL handling and text comparison ([sql-and-migrations.md](sql-and-migrations.md)).
|
|
46
57
|
- A framework `error` entry printed to stderr during a test shows the real cause of a 500 response.
|
|
47
|
-
- A failing deployed smoke test prints the method, path, status, error code and request ID; find the request's entry with [troubleshooting.md](troubleshooting.md).
|
|
58
|
+
- A failing deployed smoke test prints the method, path, status, error code and request ID; find the request's entry with [troubleshooting.md](troubleshooting.md). Then follow [testing/failures.md](testing/failures.md).
|
|
48
59
|
|
|
49
60
|
## Examples
|
|
50
61
|
|
|
@@ -52,4 +63,4 @@ To run it on a cloud, deploy a test stage first, for example `npx prototype-fram
|
|
|
52
63
|
|
|
53
64
|
## Stopping condition
|
|
54
65
|
|
|
55
|
-
Stop reading when each route has tests for success, access level, invalid input and other users' data, the smoke test covers each capability you use, and `
|
|
66
|
+
Stop reading when each route has tests for success, access level, invalid input and other users' data, each flow has its e2e tests, the smoke test covers each capability you use, and `npm run verify` passes on both dialects. For a failure, an e2e test, a test change or fuzzing, read the one file under `testing/` linked above.
|
|
@@ -35,7 +35,7 @@ The date and time are UTC. `smoke` prints today's prefix.
|
|
|
35
35
|
|
|
36
36
|
**Dashboard (no setup; these menu names are not yet verified).** Open Storage & databases → R2 → `prototype-logs`. Then open the folders `<app>` / `<stage>` / year / month / day. The file name ends in the request ID. Open the file and choose Download to read it.
|
|
37
37
|
|
|
38
|
-
**Command line,
|
|
38
|
+
**Command line, only where both values are in the environment.** These commands need `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN` in the shell's environment, for example in a cloud session whose environment settings hold them. When the token is only in `~/.secrets/cloudflare` (the usual case on a computer, [owner-tasks.md](owner-tasks.md) §2), the variables are empty and the requests fail: use the dashboard route above instead. Never `source`, `cat` or otherwise read `~/.secrets/cloudflare` (or an old `.env.cloudflare`) to fill them in. These two `curl` commands are **verified on Cloudflare**: on a real test stage, the list command found a failed request's key and the read command returned its `CORE.INTERNAL` entry (E2E-011, Cloudflare pass 2–5; the framework repository's `validation/cloudflare/pass-2to5/SUMMARY.md` and `logs/5-trouble.log`). List one day's keys:
|
|
39
39
|
|
|
40
40
|
```sh
|
|
41
41
|
STAGE='<stage>'; DAY=$(date -u +%Y/%m/%d)
|
|
@@ -70,7 +70,7 @@ Not yet verified on Cloudflare in this phase.
|
|
|
70
70
|
```sh
|
|
71
71
|
npx wrangler tail "$APP-$STAGE" --format pretty
|
|
72
72
|
```
|
|
73
|
-
It needs `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID`
|
|
73
|
+
It needs `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` in the shell's environment, as the `curl` commands above do; with the token only in `~/.secrets/cloudflare`, use the dashboard instead (Workers & Pages → `<app>-<stage>` → Logs, menu names not yet verified). Not yet verified on Cloudflare in this phase.
|
|
74
74
|
- If entries are printed as lines starting `[prototype-framework:log]` in `wrangler tail`, the Worker has no `LOGS` binding. Run `deploy` again.
|
|
75
75
|
- Locally, entries reach `.logs/` only while `npm run dev` runs. In-process tests write no files.
|
|
76
76
|
|
|
@@ -93,27 +93,24 @@ Each code's meaning, HTTP status and whether it is retryable are in the framewor
|
|
|
93
93
|
|
|
94
94
|
## 3. Login failures
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
Production stages sign people in with Cloudflare, through the account's auth app. A failed sign-in returns to `/?authError=login_failed&requestId=<id>` and writes one `warn` entry from `AUTH`, `login failed: <reason>`, never an address or code (§1):
|
|
97
97
|
|
|
98
|
-
- `
|
|
99
|
-
- `
|
|
100
|
-
- `
|
|
101
|
-
- `
|
|
102
|
-
- `
|
|
98
|
+
- `transaction missing or expired`: the sign-in took too long, or cookies are blocked. Sign in again.
|
|
99
|
+
- `state mismatch`: two sign-ins ran at once in one browser. Sign in again.
|
|
100
|
+
- `provider refused`: Cloudflare or the auth app refused. Most often the person is not a member of the owner's Cloudflare account: invite them (Manage Account → Members, [owner-tasks.md](owner-tasks.md) §6).
|
|
101
|
+
- `code refused`: the one-time code was used, expired, or issued to another app or stage. Sign in again; if it repeats, deploy the stage again so the auth app knows its address.
|
|
102
|
+
- `auth app unavailable`: the auth app or its database was not reachable; retry. If it repeats, check on the admin app's Status page that the auth app is set up.
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
- Locally nothing is sent: read `.mail/` with `npm run mail -- last --to '<address>'`. On a test stage the mail is captured, not sent.
|
|
106
|
-
- On a production stage, look for a `MAIL` entry `mail send failed: <code>` (§1). The sender is `no-reply@<domain>`; if `deploy` or `setup-account` said the domain is not verified for sending, run `npm run setup-account -- --target cloudflare` again.
|
|
107
|
-
- A test address `sink+<tag>@t.<domain>`: read it with `npm run mail -- last --to '<address>' --target cloudflare`. Only `sink+…` addresses reach the test mailbox. Right after `setup-account` created the mailbox rule, a send may be rejected for a minute.
|
|
104
|
+
Signed out after a while, with a `warn` entry `role refresh failed (<code>)`: the app could not confirm the person's role with the auth app, so it ended the session. Sign in again once the auth app answers.
|
|
108
105
|
|
|
109
|
-
Signed in, but not admin: the
|
|
106
|
+
Signed in, but not admin (or still admin): the level comes from the person's role in the Cloudflare account ([owner-tasks.md](owner-tasks.md) §7), recorded when they last signed in. Log out and sign in again after the role changes.
|
|
110
107
|
|
|
111
108
|
## 4. Deploy, remove and setup failures
|
|
112
109
|
|
|
113
110
|
`deploy` prints the failed step, the stage's state and the next action. Follow the next action. Common messages:
|
|
114
111
|
|
|
115
|
-
- `run setup-account … first
|
|
116
|
-
- A missing `CLOUDFLARE_API_TOKEN` or `CLOUDFLARE_ACCOUNT_ID`,
|
|
112
|
+
- `run npm run cloudflare-setup first` (older releases: `run setup-account … first`), or the account's auth app is missing: run `npm run cloudflare-setup` ([owner-tasks.md](owner-tasks.md) §2). It names what is missing and sets the account up.
|
|
113
|
+
- A missing `CLOUDFLARE_API_TOKEN` or `CLOUDFLARE_ACCOUNT_ID`, a missing permission, or "R2 signing credential … missing": run `npm run cloudflare-setup`, which says what to fix ([owner-tasks.md](owner-tasks.md) §2). Setup names each permission the token lacks; add it to the token in the dashboard, or make a new token from setup's link.
|
|
117
114
|
- `NODE_USE_ENV_PROXY`: you are behind an HTTPS proxy. Put `NODE_USE_ENV_PROXY=1 ` in front of the command.
|
|
118
115
|
- `required secrets are not set: …`: this happens on the first deploy of a stage. Set each named secret with `--target cloudflare`, then run `deploy` again.
|
|
119
116
|
- An error about the number of D1 databases: an account holds at most 10, and a schema-changing deploy briefly holds two per stage. Remove unused test stages ([deploying.md](deploying.md)).
|
|
@@ -7,7 +7,7 @@ Suspected framework defects found while building this app. Each entry is reprodu
|
|
|
7
7
|
- **Status:** open
|
|
8
8
|
- **Task attempted:** <the requirement or step you were implementing>
|
|
9
9
|
- **Contract or reference consulted:** <file and section, for example `skill/reference/files.md`, "The safe upload pattern">
|
|
10
|
-
- **Minimal reproduction:** <the smallest code, and the exact command that shows the problem, for example `
|
|
10
|
+
- **Minimal reproduction:** <the smallest code, and the exact command that shows the problem, for example `npm run verify`>
|
|
11
11
|
- **Observed code or diagnostic:** <copied exactly: error code and status, the `verify` line, the request ID>
|
|
12
12
|
- **Expected:** <what the contract or reference says should happen>
|
|
13
13
|
- **Workaround not taken:** <what you could have done instead, and did not>
|
|
@@ -5,6 +5,6 @@ Files you copy and fill in. The app itself comes from the scaffolder: `skill/scr
|
|
|
5
5
|
| File | Copy to | Fill in |
|
|
6
6
|
| --- | --- | --- |
|
|
7
7
|
| [FRAMEWORK-ISSUE.md](FRAMEWORK-ISSUE.md) | `FRAMEWORK-ISSUE.md` at the app's root, when you first suspect a framework defect | One numbered entry per defect, every field (router, hard rules 6 and 7) |
|
|
8
|
-
| [production-stage.json](production-stage.json) | `config/<target>/<stage>.json` for each production stage, for example `config/cloudflare/prod.json` | `values` for keys without defaults
|
|
8
|
+
| [production-stage.json](production-stage.json) | `config/<target>/<stage>.json` for each production stage, for example `config/cloudflare/prod.json` | `values` for keys without defaults. No admins: they are the Cloudflare account's administrators ([configuration.md](../reference/configuration.md), [owner-tasks.md](../reference/owner-tasks.md)) |
|
|
9
9
|
|
|
10
10
|
Test stages need no new file: every `test-*` stage uses the template's `config/<target>/test.json`.
|