create-signet-app 0.4.8 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +133 -33
  2. package/{template-full → base/api}/api/app.ts +105 -13
  3. package/base/api/api/db/open.ts +121 -0
  4. package/{template-full → base/api}/api/db/schema.ts +34 -7
  5. package/{template-full → base/api}/api/env.ts +16 -0
  6. package/base/api/api/server.ts +163 -0
  7. package/{template-full → base/api}/api/services.ts +103 -10
  8. package/base/api/drizzle/0002_ai_paywalls_workflows.sql +77 -0
  9. package/base/api/drizzle/meta/0002_snapshot.json +1759 -0
  10. package/{template-full → base/api}/drizzle/meta/_journal.json +7 -0
  11. package/base/api/test/api.test.ts +51 -0
  12. package/base/api/test/helpers.ts +51 -0
  13. package/base/both/scripts/dev.ts +44 -0
  14. package/base/shared/README.md +385 -0
  15. package/{template-full → base/shared}/_github/workflows/ci.yml +1 -1
  16. package/{template-full → base/web}/src/lib/api.ts +28 -1
  17. package/{template-full → base/web}/src/main.tsx +4 -1
  18. package/{template-full → base/web}/src/routeTree.gen.ts +155 -90
  19. package/{template-full → base/web}/src/router.test.ts +13 -9
  20. package/base/web/src/routes/_app/index.tsx +55 -0
  21. package/{template-full → base/web}/src/routes/_app.tsx +40 -16
  22. package/{template-full → base/web}/vite.config.ts +3 -1
  23. package/package.json +9 -5
  24. package/parts/account/api/test/account.test.ts +30 -0
  25. package/parts/account/part.ts +6 -0
  26. package/parts/admin/api/test/admin.test.ts +35 -0
  27. package/parts/admin/part.ts +6 -0
  28. package/{template-full → parts/admin/web}/src/routes/_app/admin.tsx +2 -1
  29. package/parts/ai/api/api/routes/ai.ts +28 -0
  30. package/parts/ai/api/test/ai.test.ts +52 -0
  31. package/parts/ai/part.ts +16 -0
  32. package/{template-full → parts/auth/api}/api/routes/account.ts +32 -3
  33. package/{template-full → parts/auth/api}/api/routes/auth.ts +5 -3
  34. package/parts/auth/api/test/auth.test.ts +28 -0
  35. package/{template-full → parts/auth/both}/e2e/smoke.spec.ts +21 -7
  36. package/parts/auth/part.ts +22 -0
  37. package/parts/growth/part.ts +7 -0
  38. package/{template-full/_mcp → parts/mcp/api}/test/mcp.test.ts +25 -16
  39. package/parts/mcp/part.ts +8 -0
  40. package/parts/notifications/api/test/notifications.test.ts +18 -0
  41. package/parts/notifications/part.ts +6 -0
  42. package/{template-full → parts/notifications/web}/src/routes/_app/notifications.tsx +1 -1
  43. package/parts/orgs/api/test/orgs.test.ts +56 -0
  44. package/parts/orgs/part.ts +7 -0
  45. package/{template-full → parts/payments/api}/api/routes/billing.ts +3 -2
  46. package/parts/payments/api/test/payments.test.ts +31 -0
  47. package/parts/payments/part.ts +12 -0
  48. package/{template-full → parts/payments/web}/src/routes/_app/billing.tsx +6 -3
  49. package/parts/paywalls/api/api/routes/paywall.ts +126 -0
  50. package/parts/paywalls/api/test/paywalls.test.ts +71 -0
  51. package/parts/paywalls/part.ts +8 -0
  52. package/parts/privacy/api/test/privacy.test.ts +20 -0
  53. package/parts/privacy/part.ts +7 -0
  54. package/parts/workflows/api/api/workflows.ts +33 -0
  55. package/parts/workflows/api/test/workflows.test.ts +44 -0
  56. package/parts/workflows/part.ts +7 -0
  57. package/parts/workflows/web/src/routes/_app/approvals.tsx +34 -0
  58. package/src/compose.ts +426 -0
  59. package/src/desktop.ts +10 -10
  60. package/src/index.ts +407 -174
  61. package/template-api/_targets/deno/Dockerfile +12 -0
  62. package/template-api/_targets/deno/_dockerignore +10 -0
  63. package/template-api/_targets/deno/src/server.ts +43 -0
  64. package/template-full/README.md +0 -166
  65. package/template-full/api/db/open.ts +0 -33
  66. package/template-full/api/server.ts +0 -50
  67. package/template-full/scripts/dev.ts +0 -17
  68. package/template-full/src/routes/_app/index.tsx +0 -33
  69. package/template-full/test/api.test.ts +0 -238
  70. package/template-full/tsconfig.json +0 -9
  71. /package/{template-full → base/api}/api/db/index.ts +0 -0
  72. /package/{template-full → base/api}/api/db/kv.ts +0 -0
  73. /package/{template-full → base/api}/drizzle/0000_init.sql +0 -0
  74. /package/{template-full → base/api}/drizzle/0001_orgs.sql +0 -0
  75. /package/{template-full → base/api}/drizzle/meta/0000_snapshot.json +0 -0
  76. /package/{template-full → base/api}/drizzle/meta/0001_snapshot.json +0 -0
  77. /package/{template-full → base/api}/drizzle.config.ts +0 -0
  78. /package/{template-full → base/api}/tsconfig.api.json +0 -0
  79. /package/{template-full → base/shared}/_gitignore +0 -0
  80. /package/{template-full → base/shared}/biome.json +0 -0
  81. /package/{template-full → base/web}/index.html +0 -0
  82. /package/{template-full → base/web}/src/app.css +0 -0
  83. /package/{template-full → base/web}/src/components/toaster.tsx +0 -0
  84. /package/{template-full → base/web}/src/router.ts +0 -0
  85. /package/{template-full → base/web}/src/routes/__root.tsx +0 -0
  86. /package/{template-full → base/web}/tsconfig.app.json +0 -0
  87. /package/{template-full → base/web}/tsconfig.node.json +0 -0
  88. /package/{template-full → base/web}/tsconfig.test.json +0 -0
  89. /package/{template-full → parts/account/web}/src/routes/_app/account.tsx +0 -0
  90. /package/{template-full → parts/auth/both}/playwright.config.ts +0 -0
  91. /package/{template-full → parts/auth/web}/src/routes/login.tsx +0 -0
  92. /package/{template-full → parts/notifications/api}/api/routes/notifications.ts +0 -0
  93. /package/{template-full → parts/orgs/web}/src/routes/_app/org/index.tsx +0 -0
  94. /package/{template-full → parts/orgs/web}/src/routes/_app/org/members.tsx +0 -0
  95. /package/{template-full → parts/orgs/web}/src/routes/_app/org/new.tsx +0 -0
  96. /package/{template-full → parts/orgs/web}/src/routes/invite.$token.tsx +0 -0
  97. /package/{template-full → parts/payments/api}/api/plans.ts +0 -0
  98. /package/{template-full → parts/privacy/api}/api/routes/privacy.ts +0 -0
  99. /package/{template-full → parts/privacy/web}/src/routes/_app/privacy.tsx +0 -0
@@ -3,11 +3,13 @@ import tailwindcss from "@tailwindcss/vite"
3
3
  import { tanstackRouter } from "@tanstack/router-plugin/vite"
4
4
  import react from "@vitejs/plugin-react"
5
5
  import { defineConfig } from "vite"
6
+ // api:begin
6
7
 
7
8
  // The API (api/server.ts) listens on PORT, 3000 by default. In dev, Vite
8
9
  // proxies /api and /mcp to it, so the browser sees one origin, as it does in
9
10
  // production where api/server.ts also serves the built app.
10
11
  const api = `http://localhost:${process.env.PORT ?? 3000}`
12
+ // api:end
11
13
 
12
14
  export default defineConfig({
13
15
  plugins: [
@@ -20,5 +22,5 @@ export default defineConfig({
20
22
  resolve: {
21
23
  alias: { "@": path.resolve(import.meta.dirname, "./src") },
22
24
  },
23
- server: { proxy: { "/api": api, "/mcp": api } },
25
+ server: { proxy: { "/api": api, "/mcp": api } }, // @api
24
26
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-signet-app",
3
- "version": "0.4.8",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "description": "Scaffold a Vite + React + Tailwind app wired to Signet.",
6
6
  "keywords": [
@@ -30,20 +30,24 @@
30
30
  "src",
31
31
  "template",
32
32
  "template-api",
33
- "template-full",
34
33
  "template-cli",
35
34
  "template-extension",
36
- "template-desktop"
35
+ "template-desktop",
36
+ "base",
37
+ "parts"
37
38
  ],
38
39
  "scripts": {
39
40
  "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
40
- "test": "bun run test/scaffold.ts && bun run test/scaffold-api.ts && bun run test/scaffold-full.ts && bun run test/scaffold-cli.ts && bun run test/scaffold-extension.ts && bun run test/scaffold-desktop.ts && bun run test/describe.ts"
41
+ "test": "bun run test/scaffold.ts && bun run test/scaffold-api.ts && bun run test/scaffold-full.ts && bun run test/scaffold-cli.ts && bun run test/scaffold-extension.ts && bun run test/scaffold-desktop.ts && bun run test/describe.ts",
42
+ "test:parts": "bun run test/scaffold-parts.ts",
43
+ "test:pm": "bun run test/scaffold-pm.ts",
44
+ "test:runtime": "bun run test/scaffold-runtime.ts"
41
45
  },
42
46
  "dependencies": {
43
47
  "@dapplelight/signet-mcp": "^0.1.5"
44
48
  },
45
49
  "devDependencies": {
46
- "@types/bun": "^1.4.2",
50
+ "@types/bun": "^1.4.4",
47
51
  "typescript": "^7.0.2"
48
52
  }
49
53
  }
@@ -0,0 +1,30 @@
1
+ import { expect, test } from "bun:test"
2
+ import { app, post, signIn } from "./helpers.ts"
3
+
4
+ test("name a profile; an API key is shown once, listed, then revoked", async () => {
5
+ const cookie = await signIn("+919000000014")
6
+ const named = await app.request("/api/account/me", {
7
+ method: "PATCH",
8
+ headers: { "content-type": "application/json", cookie, origin: "http://localhost" },
9
+ body: JSON.stringify({ name: "Asha" }),
10
+ })
11
+ expect(((await named.json()) as { user: { name: string } }).user.name).toBe("Asha")
12
+
13
+ const created = await post("/api/account/api-keys", { name: "ci" }, cookie)
14
+ expect(created.status).toBe(201)
15
+ const { token, key } = (await created.json()) as { token: string; key: { id: string } }
16
+ expect(token.startsWith("app")).toBe(true)
17
+ const list = async () =>
18
+ (
19
+ (await (await app.request("/api/account/api-keys", { headers: { cookie } })).json()) as {
20
+ keys: { id: string }[]
21
+ }
22
+ ).keys.map((k) => k.id)
23
+ expect(await list()).toEqual([key.id])
24
+ const revoke = await app.request(`/api/account/api-keys/${key.id}`, {
25
+ method: "DELETE",
26
+ headers: { cookie, origin: "http://localhost" },
27
+ })
28
+ expect(revoke.status).toBe(200)
29
+ expect(await list()).toEqual([])
30
+ })
@@ -0,0 +1,6 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "the account page: profile, signed-in devices, API keys",
5
+ needs: ["auth"],
6
+ } satisfies Part
@@ -0,0 +1,35 @@
1
+ import { expect, test } from "bun:test"
2
+ import { eq } from "drizzle-orm"
3
+ import { users } from "../api/db/schema.ts"
4
+ import { ADMIN, app, db, signIn } from "./helpers.ts"
5
+
6
+ test("admin is for ADMIN_PHONES (and other staff roles) only", async () => {
7
+ const user = await signIn("+919000000003")
8
+ expect((await app.request("/api/admin", { headers: { cookie: user } })).status).toBe(403)
9
+ const admin = await signIn(ADMIN)
10
+ const res = await app.request("/api/admin/users", { headers: { cookie: admin } })
11
+ expect(res.status).toBe(200)
12
+ expect(((await res.json()) as { rows: unknown[] }).rows.length).toBeGreaterThan(0)
13
+ })
14
+
15
+ test("staff cannot change a role or phone; an admin can", async () => {
16
+ const staff = await signIn("+919000000006")
17
+ const staffRow = db.orm.select().from(users).where(eq(users.phone, "+919000000006")).get()
18
+ db.orm.update(users).set({ role: "staff" }).where(eq(users.phone, "+919000000006")).run()
19
+ const patch = (cookie: string, id: string, body: unknown) =>
20
+ app.request(`/api/admin/users/${id}`, {
21
+ method: "PATCH",
22
+ headers: { "content-type": "application/json", cookie },
23
+ body: JSON.stringify(body),
24
+ })
25
+ const id = staffRow?.id ?? ""
26
+
27
+ expect((await patch(staff, id, { role: "admin" })).status).toBe(403)
28
+ expect((await patch(staff, id, { phone: "+919000000099" })).status).toBe(403)
29
+ expect((await patch(staff, id, { name: "Staff" })).status).toBe(200)
30
+ expect(db.orm.select().from(users).where(eq(users.id, id)).get()?.role).toBe("staff")
31
+
32
+ const admin = await signIn(ADMIN)
33
+ expect((await patch(admin, id, { role: "readonly" })).status).toBe(200)
34
+ expect(db.orm.select().from(users).where(eq(users.id, id)).get()?.role).toBe("readonly")
35
+ })
@@ -0,0 +1,6 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "the operator's admin panel over every table, by platform role",
5
+ needs: ["auth", "orgs"],
6
+ } satisfies Part
@@ -20,7 +20,8 @@ export const Route = createFileRoute("/_app/admin")({
20
20
  // panel reads its tables and columns from that API. Drop a part, reorder
21
21
  // them, or add your own inside <AdminPanel> (useAdmin() reads its state);
22
22
  // <AdminScreen api={adminApi} /> is the same layout in one line.
23
- const adminApi = createAdminApi({ baseUrl: "/api/admin" })
23
+ const adminApi = createAdminApi({ baseUrl: "/api/admin" }) // @api
24
+ const adminApi = createAdminApi({ baseUrl: `${import.meta.env.VITE_API_URL ?? ""}/api/admin` }) // @!api
24
25
 
25
26
  function Admin() {
26
27
  return (
@@ -0,0 +1,28 @@
1
+ import { spendKey } from "@dapplelight/signet-db/ai-spend"
2
+ import { zValidator } from "@dapplelight/signet-server/http/validate"
3
+ import { Hono } from "hono"
4
+ import { z } from "zod"
5
+ import type { AppEnv } from "../app.ts"
6
+ import type { Services } from "../services.ts"
7
+
8
+ /**
9
+ * AI routes. app.ts refuses any /api/ai call once the caller's or the app's
10
+ * spend cap is reached (requireAiSpend, 429 with Retry-After); each call is
11
+ * charged to the caller through `spendKey`. Copy this one for your own.
12
+ *
13
+ * POST /summary { text } → { summary, costUsd }
14
+ */
15
+ export const aiRoutes = (s: Services) =>
16
+ new Hono<AppEnv>().post(
17
+ "/summary",
18
+ zValidator("json", z.object({ text: z.string().min(1).max(10_000) })),
19
+ async (c) => {
20
+ const { value, meta } = await s.ai.extract({
21
+ schema: z.object({ summary: z.string() }),
22
+ instructions: "Summarise the text in one sentence.",
23
+ input: c.req.valid("json").text,
24
+ budget: { key: spendKey({ userId: c.get("user").id }) },
25
+ })
26
+ return c.json({ summary: value.summary, costUsd: meta.costUsd })
27
+ },
28
+ )
@@ -0,0 +1,52 @@
1
+ import { expect, test } from "bun:test"
2
+ import { eq } from "drizzle-orm" // @admin
3
+ import { spendCaps, users } from "../api/db/schema.ts" // @admin
4
+ import { spendCaps } from "../api/db/schema.ts" // @!admin
5
+ import { app, db, post, signIn } from "./helpers.ts"
6
+
7
+ const userId = async (cookie: string) => {
8
+ const me = await app.request("/api/account/me", { headers: { cookie } })
9
+ return ((await me.json()) as { user: { id: string } }).user.id
10
+ }
11
+
12
+ test("an AI call answers with no provider key, charged to the caller", async () => {
13
+ expect((await post("/api/ai/summary", { text: "Some text." })).status).toBe(401)
14
+ const cookie = await signIn("+919000000041")
15
+ const res = await post("/api/ai/summary", { text: "Some text to sum up." }, cookie)
16
+ expect(res.status).toBe(200)
17
+ expect(await res.json()).toEqual({ summary: "Some text to sum up.", costUsd: 0.001 })
18
+ })
19
+
20
+ test("over the caller's cap: 429 with Retry-After, and the route does not run", async () => {
21
+ const cookie = await signIn("+919000000042")
22
+ const id = await userId(cookie)
23
+ db.orm
24
+ .insert(spendCaps)
25
+ .values({ id: `user:${id}:day`, scope: "user", subject: id, period: "day", units: 0 })
26
+ .run()
27
+ const res = await post("/api/ai/summary", { text: "Some text." }, cookie)
28
+ expect(res.status).toBe(429)
29
+ expect(Number(res.headers.get("retry-after"))).toBeGreaterThan(0)
30
+ expect(((await res.json()) as { code: string }).code).toBe("AI_SPEND_CAP_EXCEEDED")
31
+ })
32
+ // admin:begin
33
+
34
+ test("admins read AI spend and set caps; everyone else gets 403", async () => {
35
+ const user = await signIn("+919000000043")
36
+ const headers = { cookie: user }
37
+ expect((await app.request("/api/admin/ai-spend/usage", { headers })).status).toBe(403)
38
+ const admin = await signIn("+919000000044")
39
+ db.orm.update(users).set({ role: "admin" }).where(eq(users.phone, "+919000000044")).run()
40
+ const cap = { scope: "user", subject: "*", period: "month", units: 50_000_000 }
41
+ const put = await app.request("/api/admin/ai-spend/caps", {
42
+ method: "PUT",
43
+ headers: { "content-type": "application/json", cookie: admin },
44
+ body: JSON.stringify(cap),
45
+ })
46
+ expect(put.status).toBe(200)
47
+ const caps = (await (
48
+ await app.request("/api/admin/ai-spend/caps", { headers: { cookie: admin } })
49
+ ).json()) as { caps: { id: string }[] }
50
+ expect(caps.caps.map((c) => c.id)).toContain("user:*:month")
51
+ })
52
+ // admin:end
@@ -0,0 +1,16 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "AI spend caps per user and app, and a sample /api/ai route on signet-ai",
5
+ needs: ["auth"],
6
+ apiOnly: true,
7
+ // signet-ai's main entry needs signet-lib (its error type) as a peer.
8
+ api: { deps: { "@dapplelight/signet-ai": "^0.5.0", "@dapplelight/signet-lib": "^0.4.0" } },
9
+ env: () => [
10
+ "# AI spend caps in cost units (1000000 = one US dollar); a cap stored in ai_spend_caps wins.",
11
+ "AI_CAP_USER_DAY=200000",
12
+ "AI_CAP_GLOBAL_MONTH=50000000",
13
+ ],
14
+ // usage_counters is payments' table too: it stays when either part is chosen.
15
+ tables: ["usage_counters", "ai_spend_caps"],
16
+ } satisfies Part
@@ -1,28 +1,56 @@
1
+ // account:begin
1
2
  import { createApiKey, listApiKeys, revokeApiKey } from "@dapplelight/signet-server/auth/api-keys"
2
3
  import { ApiException } from "@dapplelight/signet-server/http"
3
4
  import { zValidator } from "@dapplelight/signet-server/http/validate"
4
5
  import { eq } from "drizzle-orm"
6
+ // account:end
5
7
  import { Hono } from "hono"
6
- import { z } from "zod"
8
+ import { z } from "zod" // @account
7
9
  import type { AppEnv } from "../app.ts"
8
- import { users } from "../db/schema.ts"
10
+ import { users } from "../db/schema.ts" // @account
9
11
  import type { Services } from "../services.ts"
10
12
  import { publicUser } from "./auth.ts"
11
13
 
12
14
  /**
13
- * GET /me the signed-in user and their plan
15
+ * GET /me the signed-in user and their plan // @payments
16
+ * GET /me the signed-in user // @!payments
17
+ // account:begin
14
18
  * PATCH /me { name?, email? }
15
19
  * GET /api-keys { keys }
16
20
  * POST /api-keys { name } → { token } (shown once)
17
21
  * DELETE /api-keys/:id
22
+ // account:end
18
23
  */
24
+ // payments:begin
19
25
  export const accountRoutes = (s: Services) =>
26
+ // account:begin
20
27
  new Hono<AppEnv>()
21
28
  .get("/me", async (c) => {
22
29
  const user = c.get("user")
23
30
  const { plan } = await s.entitlements.get(user.id)
24
31
  return c.json({ user: publicUser(user), plan })
25
32
  })
33
+ // account:end
34
+ // !account:begin
35
+ new Hono<AppEnv>().get("/me", async (c) => {
36
+ const user = c.get("user")
37
+ const { plan } = await s.entitlements.get(user.id)
38
+ return c.json({ user: publicUser(user), plan })
39
+ })
40
+ // !account:end
41
+ // payments:end
42
+ // !payments:begin
43
+ // account:begin
44
+ export const accountRoutes = (s: Services) =>
45
+ new Hono<AppEnv>()
46
+ .get("/me", (c) => c.json({ user: publicUser(c.get("user")) }))
47
+ // account:end
48
+ // !account:begin
49
+ export const accountRoutes = (_s: Services) =>
50
+ new Hono<AppEnv>().get("/me", (c) => c.json({ user: publicUser(c.get("user")) }))
51
+ // !account:end
52
+ // !payments:end
53
+ // account:begin
26
54
  .patch(
27
55
  "/me",
28
56
  zValidator(
@@ -61,3 +89,4 @@ export const accountRoutes = (s: Services) =>
61
89
  throw new ApiException(404, "Key not found.", "NOT_FOUND")
62
90
  return c.json({ revoked: 1 })
63
91
  })
92
+ // account:end
@@ -16,7 +16,7 @@ import {
16
16
  validateSession,
17
17
  } from "@dapplelight/signet-server/auth/sessions"
18
18
  import { ApiException } from "@dapplelight/signet-server/http"
19
- import { orgCookie } from "@dapplelight/signet-server/http/orgs"
19
+ import { orgCookie } from "@dapplelight/signet-server/http/orgs" // @orgs
20
20
  import { zValidator } from "@dapplelight/signet-server/http/validate"
21
21
  import { clientIp } from "@dapplelight/signet-server/middleware/rate-limit"
22
22
  import { eq } from "drizzle-orm"
@@ -90,7 +90,7 @@ export const authRoutes = (s: Services) =>
90
90
  .map((p) => p.trim())
91
91
  .includes(r.phone)
92
92
  let user = s.orm.select().from(users).where(eq(users.phone, r.phone)).get()
93
- const isNew = !user
93
+ const isNew = !user // @notifications
94
94
  if (!user)
95
95
  user = s.orm
96
96
  .insert(users)
@@ -112,6 +112,7 @@ export const authRoutes = (s: Services) =>
112
112
  previousToken: readSessionCookie(c.req.header("cookie"), s.sessions),
113
113
  })
114
114
  c.header("Set-Cookie", sessionCookie(token, s.sessions))
115
+ // orgs:begin
115
116
  // Everyone has an org: a personal one on first sign-in (or for an
116
117
  // account from before orgs). Sign-in lands in the first one.
117
118
  const mine = await s.orgs.listForUser(user.id)
@@ -119,7 +120,8 @@ export const authRoutes = (s: Services) =>
119
120
  mine[0]?.org ??
120
121
  (await s.orgs.create({ name: "Personal", ownerId: user.id, personal: true }))
121
122
  c.header("Set-Cookie", orgCookie(org.id, s.orgCookie), { append: true })
122
- if (isNew) await s.notifier.notify(user.id, "account.welcome", { name: maskPhone(r.phone) })
123
+ // orgs:end
124
+ if (isNew) await s.notifier.notify(user.id, "account.welcome", { name: maskPhone(r.phone) }) // @notifications
123
125
  return c.json({ user: publicUser(user) })
124
126
  },
125
127
  )
@@ -0,0 +1,28 @@
1
+ import { expect, test } from "bun:test"
2
+ import { app, post, signIn } from "./helpers.ts"
3
+
4
+ test("signed-out calls are 401 in the error envelope", async () => {
5
+ const res = await app.request("/api/account/me")
6
+ expect(res.status).toBe(401)
7
+ expect(((await res.json()) as { code: string }).code).toBe("UNAUTHORIZED")
8
+ })
9
+
10
+ test("a wrong code is refused", async () => {
11
+ await post("/api/auth/otp", { phone: "+919000000009" })
12
+ const res = await post("/api/auth/verify", { phone: "+919000000009", code: "000000" })
13
+ expect(res.status).toBe(400)
14
+ })
15
+
16
+ test("sign in, then the account answers", async () => {
17
+ const cookie = await signIn("+919000000012")
18
+ const me = (await (await app.request("/api/account/me", { headers: { cookie } })).json()) as {
19
+ user: { role: string }
20
+ }
21
+ expect(me.user.role).toBe("user")
22
+ })
23
+
24
+ test("sign-out revokes the session", async () => {
25
+ const cookie = await signIn("+919000000005")
26
+ await post("/api/auth/sign-out", {}, cookie)
27
+ expect((await app.request("/api/account/me", { headers: { cookie } })).status).toBe(401)
28
+ })
@@ -1,13 +1,13 @@
1
1
  import { type ChildProcess, spawn } from "node:child_process"
2
2
  import { expect, test } from "@playwright/test"
3
3
 
4
- // Sign in → admin → billing in a real browser against the built app, served by
5
- // api/server.ts with a throwaway database and fake billing. The one-time code
4
+ // Sign in, then each page the app has, in a real browser against the built
5
+ // app, served by api/server.ts with a throwaway database. The one-time code
6
6
  // is read from the server's console, where development prints it. No keys.
7
7
  const PORT = 4100 + Math.floor(Math.random() * 500)
8
8
  const BASE = `http://localhost:${PORT}`
9
9
  const PHONE = "+919000000001"
10
- const PHONE_2 = "+919000000002"
10
+ const PHONE_2 = "+919000000002" // @orgs
11
11
 
12
12
  let server: ChildProcess
13
13
  let log = ""
@@ -22,7 +22,7 @@ test.beforeAll(async () => {
22
22
  SESSION_SECRET: "e2e-session-secret-0123456789abcdef",
23
23
  OTP_SECRET: "e2e-otp-secret-0123456789abcdef01234",
24
24
  ADMIN_PHONES: PHONE,
25
- BILLING: "fake",
25
+ BILLING: "fake", // @payments
26
26
  },
27
27
  })
28
28
  server.stdout?.on("data", (d) => {
@@ -47,7 +47,7 @@ test.afterAll(() => {
47
47
  server?.kill()
48
48
  })
49
49
 
50
- test("sign in, then admin, then billing", async ({ page }) => {
50
+ test("sign in, then each page", async ({ page }) => {
51
51
  await page.goto(`${BASE}/`)
52
52
  await expect(page).toHaveURL(/\/login$/)
53
53
 
@@ -58,12 +58,16 @@ test("sign in, then admin, then billing", async ({ page }) => {
58
58
  // A full code submits itself.
59
59
  await page.getByRole("textbox", { name: "One-time code" }).fill(code)
60
60
  await expect(page).toHaveURL(`${BASE}/`)
61
- await expect(page.getByText(/You are on the/)).toBeVisible()
61
+ await expect(page.getByText(/You are on the/)).toBeVisible() // @payments
62
+ await expect(page.getByText(/Replace this page/)).toBeVisible() // @!payments
63
+ // admin:begin
62
64
 
63
65
  await page.getByRole("link", { name: "Admin" }).click()
64
66
  // The admin panel loads the users table from /api/admin.
65
67
  await expect(page.getByRole("heading", { name: "Users" })).toBeVisible()
66
68
  await expect(page.getByRole("cell", { name: PHONE })).toBeVisible()
69
+ // admin:end
70
+ // payments:begin
67
71
 
68
72
  await page.getByRole("link", { name: "Billing" }).click()
69
73
  // The plan picker marks the current plan, then checkout moves it.
@@ -71,16 +75,25 @@ test("sign in, then admin, then billing", async ({ page }) => {
71
75
  await expect(current).toHaveAttribute("data-plan", "free")
72
76
  await page.getByRole("button", { name: "Continue with Pro" }).click()
73
77
  await expect(current).toHaveAttribute("data-plan", "pro")
78
+ // payments:end
79
+ // notifications:begin
74
80
 
75
81
  await page.getByRole("link", { name: /inbox/i }).click()
76
- await expect(page.getByText("Plan changed")).toBeVisible()
82
+ await expect(page.getByText("Plan changed")).toBeVisible() // @payments
83
+ await expect(page.getByText("Welcome")).toBeVisible() // @!payments
84
+ // notifications:end
85
+ // account:begin
77
86
 
78
87
  // The other wired screens render with data, not the error boundary.
79
88
  await page.getByRole("link", { name: "Account" }).click()
80
89
  await expect(page.getByText("This device")).toBeVisible()
90
+ // account:end
91
+ // privacy:begin
81
92
  await page.getByRole("link", { name: "Privacy" }).click()
82
93
  await expect(page.getByText("Usage analytics")).toBeVisible()
94
+ // privacy:end
83
95
  })
96
+ // orgs:begin
84
97
 
85
98
  /** Sign in on this page; reads the newest code the server printed. */
86
99
  async function signIn(page: import("@playwright/test").Page, phone: string) {
@@ -150,3 +163,4 @@ test("orgs: personal org, a second org, an invite, isolation", async ({ browser
150
163
  })
151
164
  expect(other.status()).toBe(404)
152
165
  })
166
+ // orgs:end
@@ -0,0 +1,22 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ /** A fresh 48-character secret for the generated env file. */
4
+ const secret = () => Buffer.from(crypto.getRandomValues(new Uint8Array(36))).toString("base64url")
5
+
6
+ export default {
7
+ description: "phone one-time-code sign-in, stored sessions, the login page",
8
+ // AuthScreen's code input.
9
+ web: { deps: { "input-otp": "^1.4.2" } },
10
+ // The browser smoke test: sign in, then each page the app has.
11
+ both: {
12
+ devDeps: { "@playwright/test": "^1.65.1" },
13
+ scripts: { "test:e2e": "playwright test" },
14
+ },
15
+ env: () => [
16
+ `SESSION_SECRET=${secret()}`,
17
+ `OTP_SECRET=${secret()}`,
18
+ "# Phone numbers (E.164, comma-separated) that become admins on sign-in.",
19
+ "ADMIN_PHONES=",
20
+ ],
21
+ tables: ["users"],
22
+ } satisfies Part
@@ -0,0 +1,7 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "waitlist, invites, referrals, feedback and marks in your own database",
5
+ apiOnly: true,
6
+ tables: ["waitlist", "invites", "referral_codes", "referrals", "marks", "feedback"],
7
+ } satisfies Part
@@ -1,23 +1,8 @@
1
1
  import { expect, test } from "bun:test"
2
+ import { app } from "./helpers.ts"
2
3
 
3
4
  // The MCP endpoint at /mcp: every zValidator route is a tool that runs as the
4
5
  // caller, and a write asks the person first.
5
- // The same values as api.test.ts: bun test runs both in one process, and the
6
- // env is validated once.
7
- Object.assign(process.env, {
8
- APP_ENV: "development",
9
- SESSION_SECRET: "test-session-secret-0123456789abcdef",
10
- OTP_SECRET: "test-otp-secret-0123456789abcdef0123",
11
- ADMIN_PHONES: "+919000000001",
12
- BILLING: "fake",
13
- })
14
- const { createApp } = await import("../api/app.ts")
15
- const { openDb } = await import("../api/db/open.ts")
16
- const { sqlKv } = await import("../api/db/kv.ts")
17
-
18
- const { db, query } = openDb(":memory:")
19
- const app = createApp({ db, query, kv: sqlKv(db.orm) })
20
-
21
6
  const rpc = async (method: string, params = {}) => {
22
7
  const res = await app.request("/mcp", {
23
8
  method: "POST",
@@ -40,6 +25,7 @@ const rpc = async (method: string, params = {}) => {
40
25
  return data ? JSON.parse(data) : null
41
26
  }
42
27
 
28
+ // payments:begin
43
29
  test("validated routes are MCP tools", async () => {
44
30
  const names = (await rpc("tools/list")).result.tools.map((t: { name: string }) => t.name)
45
31
  expect(names).toContain("post_api_billing_checkout")
@@ -53,3 +39,26 @@ test("a write tool asks for approval first, and does nothing without it", async
53
39
  expect(body.result.isError).toBe(true)
54
40
  expect(body.result.structuredContent.code).toBe("APPROVAL_REQUIRED")
55
41
  })
42
+ // payments:end
43
+ // !payments:begin
44
+ // auth:begin
45
+ test("validated routes are MCP tools", async () => {
46
+ const names = (await rpc("tools/list")).result.tools.map((t: { name: string }) => t.name)
47
+ expect(names).toContain("post_api_auth_otp")
48
+ })
49
+
50
+ test("a write tool asks for approval first, and does nothing without it", async () => {
51
+ const body = await rpc("tools/call", {
52
+ name: "post_api_auth_otp",
53
+ arguments: { phone: "+919000000099" },
54
+ })
55
+ expect(body.result.isError).toBe(true)
56
+ expect(body.result.structuredContent.code).toBe("APPROVAL_REQUIRED")
57
+ })
58
+ // auth:end
59
+ // !auth:begin
60
+ test("/mcp answers tools/list; each zValidator route you add shows up here", async () => {
61
+ expect(Array.isArray((await rpc("tools/list")).result.tools)).toBe(true)
62
+ })
63
+ // !auth:end
64
+ // !payments:end
@@ -0,0 +1,8 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "serve the API's validated routes to agents at /mcp",
5
+ apiOnly: true,
6
+ // signet-server's optional peer for /mcp.
7
+ api: { deps: { "@modelcontextprotocol/server": "^2.4.0" } },
8
+ } satisfies Part
@@ -0,0 +1,18 @@
1
+ import { expect, test } from "bun:test"
2
+ import { app, post, signIn } from "./helpers.ts"
3
+
4
+ test("a new account gets a welcome in the inbox; reading it clears the count", async () => {
5
+ const cookie = await signIn("+919000000013")
6
+ // Sends are deferred; the welcome lands in the inbox shortly.
7
+ await Bun.sleep(100)
8
+ const inbox = async () =>
9
+ (await (await app.request("/api/notifications", { headers: { cookie } })).json()) as {
10
+ items: { id: string; event: string }[]
11
+ unreadCount: number
12
+ }
13
+ const first = await inbox()
14
+ expect(first.items.map((i) => i.event)).toEqual(["account.welcome"])
15
+ expect(first.unreadCount).toBe(1)
16
+ expect((await post("/api/notifications/read-all", {}, cookie)).status).toBe(200)
17
+ expect((await inbox()).unreadCount).toBe(0)
18
+ })
@@ -0,0 +1,6 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "the in-app inbox and notification preferences",
5
+ needs: ["auth"],
6
+ } satisfies Part
@@ -13,7 +13,7 @@ export const Route = createFileRoute("/_app/notifications")({
13
13
  // Categories match the `category` of each template in api/services.ts.
14
14
  const CATEGORIES = [
15
15
  { id: "account", label: "Account", description: "Sign-ins and profile changes" },
16
- { id: "billing", label: "Billing", description: "Plan changes and receipts", required: true },
16
+ { id: "billing", label: "Billing", description: "Plan changes and receipts", required: true }, // @payments
17
17
  ]
18
18
 
19
19
  function Notifications() {