create-signet-app 0.4.7 → 0.5.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 (92) hide show
  1. package/README.md +160 -28
  2. package/{template-full → base/api}/api/app.ts +93 -15
  3. package/base/api/api/db/open.ts +121 -0
  4. package/{template-full → base/api}/api/db/schema.ts +22 -7
  5. package/{template-full → base/api}/api/env.ts +13 -0
  6. package/base/api/api/server.ts +157 -0
  7. package/{template-full → base/api}/api/services.ts +77 -8
  8. package/base/api/test/api.test.ts +51 -0
  9. package/base/api/test/helpers.ts +51 -0
  10. package/base/both/scripts/dev.ts +44 -0
  11. package/base/shared/README.md +298 -0
  12. package/{template-full → base/shared}/_github/workflows/ci.yml +1 -1
  13. package/{template-full → base/shared}/biome.json +1 -1
  14. package/{template-full → base/web}/src/lib/api.ts +28 -1
  15. package/{template-full → base/web}/src/main.tsx +4 -1
  16. package/{template-full → base/web}/src/routeTree.gen.ts +130 -90
  17. package/{template-full → base/web}/src/router.test.ts +12 -9
  18. package/base/web/src/routes/_app/index.tsx +55 -0
  19. package/{template-full → base/web}/src/routes/_app.tsx +39 -16
  20. package/{template-full → base/web}/vite.config.ts +3 -1
  21. package/package.json +9 -5
  22. package/parts/account/api/test/account.test.ts +30 -0
  23. package/parts/account/part.ts +6 -0
  24. package/parts/admin/api/test/admin.test.ts +35 -0
  25. package/parts/admin/part.ts +6 -0
  26. package/{template-full → parts/admin/web}/src/routes/_app/admin.tsx +2 -1
  27. package/{template-full → parts/auth/api}/api/routes/account.ts +32 -3
  28. package/{template-full → parts/auth/api}/api/routes/auth.ts +5 -3
  29. package/parts/auth/api/test/auth.test.ts +28 -0
  30. package/{template-full → parts/auth/both}/e2e/smoke.spec.ts +21 -7
  31. package/parts/auth/part.ts +22 -0
  32. package/parts/growth/part.ts +7 -0
  33. package/{template-full/_mcp → parts/mcp/api}/test/mcp.test.ts +25 -16
  34. package/parts/mcp/part.ts +8 -0
  35. package/parts/notifications/api/test/notifications.test.ts +18 -0
  36. package/parts/notifications/part.ts +6 -0
  37. package/{template-full → parts/notifications/web}/src/routes/_app/notifications.tsx +1 -1
  38. package/parts/orgs/api/test/orgs.test.ts +56 -0
  39. package/parts/orgs/part.ts +7 -0
  40. package/{template-full → parts/payments/api}/api/routes/billing.ts +3 -2
  41. package/parts/payments/api/test/payments.test.ts +31 -0
  42. package/parts/payments/part.ts +12 -0
  43. package/{template-full → parts/payments/web}/src/routes/_app/billing.tsx +6 -3
  44. package/parts/privacy/api/test/privacy.test.ts +20 -0
  45. package/parts/privacy/part.ts +7 -0
  46. package/src/compose.ts +369 -0
  47. package/src/describe.ts +200 -0
  48. package/src/desktop.ts +10 -10
  49. package/src/index.ts +433 -159
  50. package/template/biome.json +1 -1
  51. package/template-api/_targets/deno/Dockerfile +12 -0
  52. package/template-api/_targets/deno/_dockerignore +10 -0
  53. package/template-api/_targets/deno/src/server.ts +43 -0
  54. package/template-api/biome.json +1 -1
  55. package/template-cli/biome.json +1 -1
  56. package/template-extension/biome.json +1 -1
  57. package/template-full/README.md +0 -155
  58. package/template-full/api/db/open.ts +0 -33
  59. package/template-full/api/server.ts +0 -44
  60. package/template-full/scripts/dev.ts +0 -17
  61. package/template-full/src/routes/_app/index.tsx +0 -33
  62. package/template-full/test/api.test.ts +0 -189
  63. package/template-full/tsconfig.json +0 -9
  64. /package/{template-full → base/api}/api/db/index.ts +0 -0
  65. /package/{template-full → base/api}/api/db/kv.ts +0 -0
  66. /package/{template-full → base/api}/drizzle/0000_init.sql +0 -0
  67. /package/{template-full → base/api}/drizzle/0001_orgs.sql +0 -0
  68. /package/{template-full → base/api}/drizzle/meta/0000_snapshot.json +0 -0
  69. /package/{template-full → base/api}/drizzle/meta/0001_snapshot.json +0 -0
  70. /package/{template-full → base/api}/drizzle/meta/_journal.json +0 -0
  71. /package/{template-full → base/api}/drizzle.config.ts +0 -0
  72. /package/{template-full → base/api}/tsconfig.api.json +0 -0
  73. /package/{template-full → base/shared}/_gitignore +0 -0
  74. /package/{template-full → base/web}/index.html +0 -0
  75. /package/{template-full → base/web}/src/app.css +0 -0
  76. /package/{template-full → base/web}/src/components/toaster.tsx +0 -0
  77. /package/{template-full → base/web}/src/router.ts +0 -0
  78. /package/{template-full → base/web}/src/routes/__root.tsx +0 -0
  79. /package/{template-full → base/web}/tsconfig.app.json +0 -0
  80. /package/{template-full → base/web}/tsconfig.node.json +0 -0
  81. /package/{template-full → base/web}/tsconfig.test.json +0 -0
  82. /package/{template-full → parts/account/web}/src/routes/_app/account.tsx +0 -0
  83. /package/{template-full → parts/auth/both}/playwright.config.ts +0 -0
  84. /package/{template-full → parts/auth/web}/src/routes/login.tsx +0 -0
  85. /package/{template-full → parts/notifications/api}/api/routes/notifications.ts +0 -0
  86. /package/{template-full → parts/orgs/web}/src/routes/_app/org/index.tsx +0 -0
  87. /package/{template-full → parts/orgs/web}/src/routes/_app/org/members.tsx +0 -0
  88. /package/{template-full → parts/orgs/web}/src/routes/_app/org/new.tsx +0 -0
  89. /package/{template-full → parts/orgs/web}/src/routes/invite.$token.tsx +0 -0
  90. /package/{template-full → parts/payments/api}/api/plans.ts +0 -0
  91. /package/{template-full → parts/privacy/api}/api/routes/privacy.ts +0 -0
  92. /package/{template-full → parts/privacy/web}/src/routes/_app/privacy.tsx +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-signet-app",
3
- "version": "0.4.7",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "description": "Scaffold a Vite + React + Tailwind app wired to Signet.",
6
6
  "keywords": [
@@ -30,17 +30,21 @@
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"
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
+ },
43
+ "dependencies": {
44
+ "@dapplelight/signet-mcp": "^0.1.5"
41
45
  },
42
46
  "devDependencies": {
43
- "@types/bun": "^1.4.2",
47
+ "@types/bun": "^1.4.4",
44
48
  "typescript": "^7.0.2"
45
49
  }
46
50
  }
@@ -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 (
@@ -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() {
@@ -0,0 +1,56 @@
1
+ import { expect, test } from "bun:test"
2
+ import { app, codes, post } from "./helpers.ts"
3
+
4
+ test("sign-up makes a personal org; another org's data is 404", async () => {
5
+ const get = (path: string, cookie: string, org?: string) =>
6
+ app.request(path, { headers: { cookie, ...(org ? { "x-org": org } : {}) } })
7
+ const signInWithOrg = async (phone: string) => {
8
+ expect((await post("/api/auth/otp", { phone })).status).toBe(200)
9
+ const res = await post("/api/auth/verify", { phone, code: codes.get(phone) })
10
+ // Two cookies: the session, and the org the user lands in.
11
+ const cookies = res.headers.getSetCookie().map((c) => c.split(";")[0])
12
+ expect(cookies.some((c) => c?.startsWith("signet_org="))).toBe(true)
13
+ return cookies.join("; ")
14
+ }
15
+ const a = await signInWithOrg("+919000000010")
16
+ const b = await signInWithOrg("+919000000011")
17
+ const mine = (await (await get("/api/orgs", a)).json()) as {
18
+ orgs: { org: { id: string; personal: boolean }; role: string }[]
19
+ }
20
+ expect(mine.orgs).toHaveLength(1)
21
+ expect(mine.orgs[0]).toMatchObject({ role: "owner", org: { personal: true } })
22
+ const aOrg = mine.orgs[0]?.org.id as string
23
+ expect((await get("/api/orgs/current", a)).status).toBe(200)
24
+ // B pointing at A's org, by header: not found, and nothing leaks.
25
+ for (const path of [
26
+ "/api/orgs/current",
27
+ "/api/orgs/current/members",
28
+ "/api/orgs/current/invites",
29
+ ])
30
+ expect((await get(path, b, aOrg)).status).toBe(404)
31
+ const join = (await post("/api/orgs/switch", { orgId: aOrg }, b)).status
32
+ expect(join).toBe(404)
33
+
34
+ // A invites B by link; B joins A's org and sees it.
35
+ const invite = await app.request("/api/orgs/current/invites", {
36
+ method: "POST",
37
+ headers: { "content-type": "application/json", cookie: a },
38
+ body: JSON.stringify({ role: "member" }),
39
+ })
40
+ const { token } = (await invite.json()) as { token: string }
41
+ expect((await app.request(`/api/orgs/invites/${token}`)).status).toBe(200) // preview, signed out
42
+ expect((await post(`/api/orgs/invites/${token}/accept`, {}, b)).status).toBe(200)
43
+ expect((await get("/api/orgs/current/members", b, aOrg)).status).toBe(200)
44
+ // A member cannot change settings.
45
+ const patch = await app.request("/api/orgs/current", {
46
+ method: "PATCH",
47
+ headers: {
48
+ "content-type": "application/json",
49
+ cookie: b,
50
+ "x-org": aOrg,
51
+ origin: "http://localhost",
52
+ },
53
+ body: JSON.stringify({ name: "x" }),
54
+ })
55
+ expect(patch.status).toBe(403)
56
+ })
@@ -0,0 +1,7 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "organisations: a personal org each, members, roles, invite links, org pages",
5
+ needs: ["auth"],
6
+ tables: ["organizations", "memberships", "invitations", "org_roles"],
7
+ } satisfies Part
@@ -4,7 +4,8 @@ import { Hono } from "hono"
4
4
  import { z } from "zod"
5
5
  import type { AppEnv } from "../app.ts"
6
6
  import { env } from "../env.ts"
7
- import { PLAN_DISPLAY, type PlanId, plans, type Services } from "../services.ts"
7
+ import { PLAN_DISPLAY, type PlanId, plans, type Services } from "../services.ts" // @notifications
8
+ import { type PlanId, plans, type Services } from "../services.ts" // @!notifications
8
9
 
9
10
  const PLAN_IDS = Object.keys(plans) as [PlanId, ...PlanId[]]
10
11
 
@@ -33,7 +34,7 @@ export const billingRoutes = (s: Services) =>
33
34
  // webhook at /api/billing/webhooks/razorpay then moves the plan.
34
35
  throw new ApiException(409, "Real checkout is not wired yet.", "NOT_IMPLEMENTED")
35
36
  await s.entitlements.setPlan(user.id, plan, { actor: { type: "user", id: user.id } })
36
- await s.notifier.notify(user.id, "billing.plan_changed", { plan: PLAN_DISPLAY[plan].name })
37
+ await s.notifier.notify(user.id, "billing.plan_changed", { plan: PLAN_DISPLAY[plan].name }) // @notifications
37
38
  return c.json({ plan })
38
39
  })
39
40
 
@@ -0,0 +1,31 @@
1
+ import { expect, test } from "bun:test"
2
+ import { app, post, signIn } from "./helpers.ts"
3
+
4
+ test("sign in, see the plan, upgrade", async () => {
5
+ const cookie = await signIn("+919000000002")
6
+ const me = (await (await app.request("/api/account/me", { headers: { cookie } })).json()) as {
7
+ user: { role: string }
8
+ plan: string
9
+ }
10
+ expect(me).toMatchObject({ user: { role: "user" }, plan: "free" })
11
+
12
+ expect((await post("/api/billing/checkout", { plan: "pro" }, cookie)).status).toBe(200)
13
+ const billing = (await (await app.request("/api/billing", { headers: { cookie } })).json()) as {
14
+ subscription: { plan: string }
15
+ }
16
+ expect(billing.subscription.plan).toBe("pro")
17
+ // notifications:begin
18
+
19
+ // Sends are deferred; the welcome and plan notices land in the inbox shortly.
20
+ await Bun.sleep(100)
21
+ const inbox = (await (
22
+ await app.request("/api/notifications", { headers: { cookie } })
23
+ ).json()) as {
24
+ items: { event: string }[]
25
+ }
26
+ expect(inbox.items.map((i) => i.event).sort()).toEqual([
27
+ "account.welcome",
28
+ "billing.plan_changed",
29
+ ])
30
+ // notifications:end
31
+ })
@@ -0,0 +1,12 @@
1
+ import type { Part } from "../../src/compose.js"
2
+
3
+ export default {
4
+ description: "plans, entitlements, the billing page and gateway webhooks",
5
+ needs: ["auth"],
6
+ // The billing page reads the plans, which use signet-db's definePlans.
7
+ web: { deps: { "@dapplelight/signet-db": "^0.2.0" } },
8
+ api: { deps: { "@dapplelight/signet-payments": "^0.1.0" } },
9
+ env: () => ["BILLING=fake"],
10
+ tables: ["subject_plans", "entitlement_overrides", "usage_counters"],
11
+ uiCopies: { "api/plans.ts": "src/lib/plans.ts" },
12
+ } satisfies Part
@@ -5,16 +5,19 @@ import {
5
5
  import { useQuery, useQueryClient } from "@tanstack/react-query"
6
6
  import { createFileRoute } from "@tanstack/react-router"
7
7
  import { toast } from "sonner"
8
- import { PLAN_DISPLAY, type PlanId, plans } from "../../../api/plans.ts"
8
+ import { PLAN_DISPLAY, type PlanId, plans } from "../../../api/plans.ts" // @api
9
9
  import { api } from "../../lib/api"
10
+ import { PLAN_DISPLAY, type PlanId, plans } from "../../lib/plans.ts" // @!api
10
11
 
11
12
  export const Route = createFileRoute("/_app/billing")({
12
13
  component: Billing,
13
14
  })
14
15
 
15
16
  // What each plan allows (definePlans) plus what the page shows (PLAN_DISPLAY),
16
- // both from api/plans.ts. The current plan and usage come from the API, from
17
- // signet-db entitlements. With BILLING=fake, checkout switches at once.
17
+ // both from api/plans.ts. The current plan and usage come from the API, from // @api
18
+ // signet-db entitlements. With BILLING=fake, checkout switches at once. // @api
19
+ // both from src/lib/plans.ts, a copy of your API's. The current plan and usage // @!api
20
+ // come from the API. // @!api
18
21
  const PLANS = plansFromEntitlements(plans, PLAN_DISPLAY)
19
22
 
20
23
  function Billing() {