@lago-morph/create-prototype-app 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.
Files changed (42) hide show
  1. package/README.md +9 -2
  2. package/bin/create-prototype-app.js +26 -10
  3. package/lib/create.mjs +115 -2
  4. package/package.json +1 -1
  5. package/skill/SKILL.md +19 -26
  6. package/skill/VERSION +1 -0
  7. package/skill/examples/notes-sql.md +1 -1
  8. package/skill/examples/secrets.md +20 -1
  9. package/skill/examples/tests.md +109 -0
  10. package/skill/reference/configuration.md +4 -4
  11. package/skill/reference/current-user.md +10 -10
  12. package/skill/reference/deploying.md +6 -8
  13. package/skill/reference/files.md +1 -1
  14. package/skill/reference/http-and-validation.md +1 -1
  15. package/skill/reference/kv.md +1 -1
  16. package/skill/reference/local-development.md +2 -2
  17. package/skill/reference/log.md +1 -1
  18. package/skill/reference/owner-tasks.md +53 -67
  19. package/skill/reference/secrets.md +3 -3
  20. package/skill/reference/sql-and-migrations.md +3 -3
  21. package/skill/reference/testing/e2e.md +44 -0
  22. package/skill/reference/testing/failures.md +44 -0
  23. package/skill/reference/testing/fuzzing.md +28 -0
  24. package/skill/reference/testing/test-changes.md +42 -0
  25. package/skill/reference/testing.md +33 -22
  26. package/skill/reference/troubleshooting.md +12 -15
  27. package/skill/templates/FRAMEWORK-ISSUE.md +1 -1
  28. package/skill/templates/README.md +1 -1
  29. package/skill/templates/production-stage.json +1 -3
  30. package/template/.claude/settings.json +36 -0
  31. package/template/README.md +27 -66
  32. package/template/gitignore +3 -0
  33. package/template/package.json +10 -4
  34. package/template/playwright.config.ts +31 -0
  35. package/template/src/server/routes/notes.ts +1 -1
  36. package/template/src/server/signing.ts +4 -1
  37. package/template/tests/e2e/api/notes.spec.ts +74 -0
  38. package/template/tests/e2e/ui/account.spec.ts +22 -0
  39. package/template/tests/fuzz.test.ts +22 -0
  40. package/template/tests/signing.test.ts +16 -0
  41. package/template/tests/support/fuzz.test.ts +72 -0
  42. package/template/tests/support/fuzz.ts +88 -0
@@ -0,0 +1,72 @@
1
+ import { z } from "@lago-morph/prototype-framework";
2
+ import { describe, expect, it } from "vitest";
3
+ import { fuzzJsonRoute, type Requester } from "./fuzz";
4
+
5
+ /**
6
+ * The fuzz helper's own tests: fake routes that answer by rule, so each way a route can break the
7
+ * helper's oracle is shown to fail the run, and a correct route is shown to pass. Small runs with a
8
+ * fixed seed: these check the helper, not a route.
9
+ */
10
+ const schema = z.object({ text: z.string().trim().min(1).max(20) });
11
+ const RUNS = { numRuns: 100, seed: 7 };
12
+
13
+ function route(answer: (body: unknown, valid: boolean) => Response): Requester {
14
+ return {
15
+ async request(_path, init) {
16
+ const raw = typeof init?.body === "string" ? init.body : "";
17
+ let body: unknown;
18
+ try {
19
+ body = JSON.parse(raw);
20
+ } catch {
21
+ return Response.json({ error: { code: "HTTP.INVALID_INPUT" } }, { status: 400 });
22
+ }
23
+ return answer(body, schema.safeParse(body).success);
24
+ },
25
+ };
26
+ }
27
+
28
+ const invalidInput = (): Response => Response.json({ error: { code: "HTTP.INVALID_INPUT", message: "bad" } }, { status: 400 });
29
+ const created = (): Response => Response.json({ id: "n1" }, { status: 201 });
30
+
31
+ describe("fuzzJsonRoute", () => {
32
+ it("passes a route that refuses exactly the bodies its schema refuses", async () => {
33
+ await expect(fuzzJsonRoute(route((_b, valid) => (valid ? created() : invalidInput())), { path: "/api/x", schema, examples: [{ text: "hi" }], ...RUNS })).resolves.toBeUndefined();
34
+ });
35
+
36
+ it("passes when the route's own rules refuse a schema-valid body with another status", async () => {
37
+ await expect(fuzzJsonRoute(route((_b, valid) => (valid ? Response.json({ error: { code: "HTTP.CONFLICT" } }, { status: 409 }) : invalidInput())), { path: "/api/x", schema, examples: [{ text: "hi" }], ...RUNS })).resolves.toBeUndefined();
38
+ });
39
+
40
+ it("fails a route that answers a server error", async () => {
41
+ const r = route((body, valid) => (Array.isArray(body) ? new Response("boom", { status: 500 }) : valid ? created() : invalidInput()));
42
+ await expect(fuzzJsonRoute(r, { path: "/api/x", schema, examples: [{ text: "hi" }], ...RUNS })).rejects.toThrow(/500/);
43
+ });
44
+
45
+ it("fails a route that accepts a body its schema refuses", async () => {
46
+ await expect(fuzzJsonRoute(route(() => created()), { path: "/api/x", schema, examples: [{ text: "hi" }], ...RUNS })).rejects.toThrow(/schema refuses/);
47
+ });
48
+
49
+ it("fails a route that answers HTTP.INVALID_INPUT for a body its schema accepts", async () => {
50
+ await expect(fuzzJsonRoute(route(() => invalidInput()), { path: "/api/x", schema, examples: [{ text: "hi" }], ...RUNS })).rejects.toThrow(/schema accepts/);
51
+ });
52
+
53
+ it("sends the method and path given, as JSON", async () => {
54
+ const seen: string[] = [];
55
+ const correct = route((_b, valid) => (valid ? created() : invalidInput()));
56
+ const r: Requester = {
57
+ request(path, init) {
58
+ seen.push(`${init?.method ?? "GET"} ${path} ${new Headers(init?.headers).get("content-type") ?? ""}`);
59
+ return correct.request(path, init);
60
+ },
61
+ };
62
+ await fuzzJsonRoute(r, { path: "/api/y", method: "PUT", schema, examples: [{ text: "hi" }], numRuns: 5, seed: 1 });
63
+ expect(new Set(seen)).toEqual(new Set(["PUT /api/y application/json"]));
64
+ });
65
+
66
+ it("refuses an example the schema refuses, before sending anything", async () => {
67
+ const seen: string[] = [];
68
+ const r: Requester = { request: (path) => (seen.push(path), Promise.resolve(created())) };
69
+ await expect(fuzzJsonRoute(r, { path: "/api/x", schema, examples: [{ text: "" }], ...RUNS })).rejects.toThrow(/example .* is refused by the schema/);
70
+ expect(seen).toEqual([]);
71
+ });
72
+ });
@@ -0,0 +1,88 @@
1
+ import type { z } from "@lago-morph/prototype-framework";
2
+ import fc from "fast-check";
3
+
4
+ /**
5
+ * A small fuzz helper for a JSON route, driven by the route's own zod schema (the skill's
6
+ * reference/testing/fuzzing.md). Use it only on a route whose input sits at a trust boundary and
7
+ * whose example tests keep missing cases, never on every route. It sends generated bodies, any
8
+ * JSON value and changed copies of valid examples, and checks each answer against the schema:
9
+ * - never a server error (5xx);
10
+ * - a body the schema refuses gets 400 `HTTP.INVALID_INPUT`;
11
+ * - a body the schema accepts never gets `HTTP.INVALID_INPUT` (the route's own rules may still
12
+ * refuse it, for example with 404 or 409).
13
+ * A failure names the seed and the smallest failing body; add that body as a plain example test.
14
+ */
15
+
16
+ /** What the helper needs from a test app: `createTestApp`'s `request`. */
17
+ export interface Requester {
18
+ request(path: string, init?: RequestInit): Promise<Response>;
19
+ }
20
+
21
+ export interface FuzzJsonRouteOptions {
22
+ /** The route's path, with `/api`. */
23
+ readonly path: string;
24
+ /** Default POST. */
25
+ readonly method?: "POST" | "PUT" | "PATCH";
26
+ /** The schema the route passes to `jsonBody(…)`. */
27
+ readonly schema: z.ZodType;
28
+ /** At least one body the schema accepts; generated bodies change these. */
29
+ readonly examples: readonly [unknown, ...unknown[]];
30
+ /** Runs and fixed seed: state both at the call, with a one-line reason. Keep runs small. */
31
+ readonly numRuns: number;
32
+ readonly seed: number;
33
+ }
34
+
35
+ /** A value for one field: any JSON value, or a string near the usual length limits. */
36
+ const fieldValue: fc.Arbitrary<unknown> = fc.oneof(
37
+ fc.jsonValue(),
38
+ fc.string({ unit: "grapheme", maxLength: 600 }),
39
+ fc.nat({ max: 600 }).map((n) => " ".repeat(n)),
40
+ fc.constantFrom("", " ", "\u00a0", "\u200b", "a".repeat(500), "a".repeat(501)),
41
+ );
42
+
43
+ function isRecord(value: unknown): value is Record<string, unknown> {
44
+ return typeof value === "object" && value !== null && !Array.isArray(value);
45
+ }
46
+
47
+ /** A copy of `example` with one field replaced, removed or added; other values unchanged. */
48
+ function changed(example: unknown): fc.Arbitrary<unknown> {
49
+ if (!isRecord(example)) return fc.oneof(fc.constant(example), fieldValue);
50
+ const keys = Object.keys(example);
51
+ const replaced = fc.tuple(fc.constantFrom(...keys, "extra"), fieldValue).map(([key, value]) => ({ ...example, [key]: value }));
52
+ if (keys.length === 0) return replaced;
53
+ const removed = fc.constantFrom(...keys).map((key) => Object.fromEntries(Object.entries(example).filter(([k]) => k !== key)));
54
+ return fc.oneof(replaced, removed);
55
+ }
56
+
57
+ async function errorCode(res: Response): Promise<string | undefined> {
58
+ try {
59
+ const body: { error?: { code?: string } } = await res.json();
60
+ return body.error?.code;
61
+ } catch {
62
+ return undefined;
63
+ }
64
+ }
65
+
66
+ export async function fuzzJsonRoute(app: Requester, options: FuzzJsonRouteOptions): Promise<void> {
67
+ const { path, method = "POST", schema, examples, numRuns, seed } = options;
68
+ // A wrong example would leave the "never INVALID_INPUT for an accepted body" check untested.
69
+ for (const example of examples) {
70
+ if (!(await schema.safeParseAsync(example)).success) throw new Error(`fuzzJsonRoute: the example ${JSON.stringify(example)} is refused by the schema; give bodies it accepts`);
71
+ }
72
+ const body = fc.oneof(fc.constantFrom(...examples), fc.constantFrom(...examples).chain(changed), fc.jsonValue());
73
+ await fc.assert(
74
+ fc.asyncProperty(body, async (value) => {
75
+ const sent = JSON.stringify(value);
76
+ // The route sees what JSON.parse gives back, so judge that, not the generated value.
77
+ const accepted = (await schema.safeParseAsync(JSON.parse(sent))).success;
78
+ const res = await app.request(path, { method, headers: { "content-type": "application/json" }, body: sent });
79
+ if (res.status >= 500) throw new Error(`${method} ${path} answered ${String(res.status)} for ${sent}`);
80
+ const code = await errorCode(res);
81
+ if (!accepted && !(res.status === 400 && code === "HTTP.INVALID_INPUT")) {
82
+ throw new Error(`${method} ${path} answered ${String(res.status)} ${code ?? ""} for a body the schema refuses: ${sent}`);
83
+ }
84
+ if (accepted && code === "HTTP.INVALID_INPUT") throw new Error(`${method} ${path} answered HTTP.INVALID_INPUT for a body the schema accepts: ${sent}`);
85
+ }),
86
+ { numRuns, seed, includeErrorInReport: true },
87
+ );
88
+ }