Become a Pro / website · becomeapro.ai / src / app / api / account / account-route.test.ts
| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { convertV4MiniflareOptions, Miniflare } from "miniflare"; |
| 3 | import { accountStore, d1AccountStore } from "@/lib/account-store"; |
| 4 | import { sessionUser, type AccountUser } from "@/lib/session-server"; |
| 5 | import { GET, POST } from "./[section]/route"; |
| 6 | |
| 7 | jest.mock("@opennextjs/cloudflare", () => ({ getCloudflareContext: jest.fn() })); |
| 8 | jest.mock("@/lib/session-server", () => ({ sessionUser: jest.fn() })); |
| 9 | jest.mock("@/lib/account-store", () => ({ |
| 10 | ...jest.requireActual("@/lib/account-store"), |
| 11 | accountStore: jest.fn(), |
| 12 | })); |
| 13 | |
| 14 | const url = "https://airesourcepro.com/api/account/"; |
| 15 | const ownerId = "owner"; |
| 16 | const otherId = "other"; |
| 17 | const tutorial = "claude/claude-guide"; |
| 18 | const context = (section: string) => ({ params: Promise.resolve({ section }) }); |
| 19 | const user = (id: string): AccountUser => ({ |
| 20 | id, |
| 21 | name: "Test user", |
| 22 | email: id + "@example.test", |
| 23 | emailVerified: true, |
| 24 | createdAt: new Date("2026-11-25T00:00:00.000Z"), |
| 25 | updatedAt: new Date("2026-11-25T00:00:00.000Z"), |
| 26 | }); |
| 27 | |
| 28 | let runtime: Miniflare; |
| 29 | let db: Awaited<ReturnType<Miniflare["getD1Database"]>>; |
| 30 | |
| 31 | beforeAll(async () => { |
| 32 | runtime = new Miniflare(convertV4MiniflareOptions({ |
| 33 | modules: true, |
| 34 | script: "export default { fetch() { return new Response(); } }", |
| 35 | compatibilityDate: "2026-07-30", |
| 36 | d1Databases: ["ACCOUNTS_DB"], |
| 37 | })); |
| 38 | db = await runtime.getD1Database("ACCOUNTS_DB"); |
| 39 | const schema = ["0001_accounts.sql", "0004_ai_surveys.sql"].map((file) => readFileSync(`d1/migrations/${file}`, "utf-8")).join(";"); |
| 40 | const statements = schema.split(";").map((sql) => sql.replace(/^--.*$/gm, "").trim()).filter(Boolean); |
| 41 | await db.batch(statements.map((sql) => db.prepare(sql))); |
| 42 | jest.mocked(accountStore).mockImplementation((current) => d1AccountStore(db, current)); |
| 43 | }); |
| 44 | |
| 45 | afterAll(async () => { await runtime?.dispose(); }); |
| 46 | |
| 47 | function asUser(id: string | null) { |
| 48 | jest.mocked(sessionUser).mockResolvedValue(id ? user(id) : null); |
| 49 | } |
| 50 | |
| 51 | function change(section: string, body: object, origin = "https://airesourcepro.com") { |
| 52 | return POST(new Request(url + section, { |
| 53 | method: "POST", body: JSON.stringify(body), headers: { origin }, |
| 54 | }), context(section)); |
| 55 | } |
| 56 | |
| 57 | function read(section: string) { |
| 58 | return GET(new Request(url + section), context(section)); |
| 59 | } |
| 60 | |
| 61 | test("account routes refuse anonymous reads and cross-origin writes", async () => { |
| 62 | asUser(null); |
| 63 | const result = await read("saved"); |
| 64 | expect(result.status).toBe(401); |
| 65 | expect(result.headers.get("Cache-Control")).toBe("private, no-store"); |
| 66 | asUser(ownerId); |
| 67 | expect((await change("saved", { key: tutorial, saved: true }, "https://example.invalid")).status).toBe(403); |
| 68 | }); |
| 69 | |
| 70 | test("D1 preserves account isolation, idempotent saves, and tutorial activity", async () => { |
| 71 | asUser(ownerId); |
| 72 | expect((await change("saved", { key: tutorial, saved: true, user_id: otherId })).status).toBe(200); |
| 73 | expect((await change("saved", { key: tutorial, saved: true })).status).toBe(200); |
| 74 | expect(await (await read("saved")).json()).toEqual({ data: [tutorial] }); |
| 75 | const profile = { goals: ["code"], tracks: ["claude"], level: "Beginner", weeklyTime: "light", format: "both" }; |
| 76 | expect((await change("profile", { profile })).status).toBe(200); |
| 77 | expect((await (await read("profile")).json()).data).toMatchObject(profile); |
| 78 | expect((await change("progress", { keys: [tutorial], completed: true })).status).toBe(200); |
| 79 | expect((await change("view", { key: tutorial })).status).toBe(200); |
| 80 | expect((await change("view", { key: tutorial })).status).toBe(200); |
| 81 | expect(await (await read("progress")).json()).toEqual({ data: [tutorial] }); |
| 82 | expect(await db.prepare("SELECT view_count, first_viewed_at FROM tutorial_activity WHERE user_id = ?") |
| 83 | .bind(ownerId).first()).toMatchObject({ view_count: 2, first_viewed_at: expect.any(String) }); |
| 84 | |
| 85 | asUser(otherId); |
| 86 | expect((await change("saved", { key: tutorial, saved: false, user_id: ownerId })).status).toBe(200); |
| 87 | expect(await (await read("saved")).json()).toEqual({ data: [] }); |
| 88 | expect(await (await read("profile")).json()).toEqual({ data: null }); |
| 89 | expect(await (await read("progress")).json()).toEqual({ data: [] }); |
| 90 | asUser(ownerId); |
| 91 | expect(await (await read("saved")).json()).toEqual({ data: [tutorial] }); |
| 92 | expect((await change("progress", { keys: [tutorial], completed: false })).status).toBe(200); |
| 93 | expect(await (await read("progress")).json()).toEqual({ data: [] }); |
| 94 | }); |
| 95 | |
| 96 | test("AI survey answers are validated, private to each account, editable and deletable", async () => { |
| 97 | asUser(ownerId); |
| 98 | expect(await (await read("survey")).json()).toEqual({ data: null }); |
| 99 | const survey = { |
| 100 | role: "researcher", uses: ["robotics", "drug", "other", "made-up"], usesOther: " Materials science ", |
| 101 | tools: ["claude", "claude"], dailyHours: "3-6h", aiYears: "2-3y", depth: "fine-tune", coding: "bogus", |
| 102 | }; |
| 103 | expect((await change("survey", { survey, user_id: otherId })).status).toBe(200); |
| 104 | const saved = (await (await read("survey")).json()).data; |
| 105 | expect(saved).toMatchObject({ |
| 106 | role: "researcher", uses: ["robotics", "drug", "other"], usesOther: "Materials science", |
| 107 | tools: ["claude"], dailyHours: "3-6h", aiYears: "2-3y", depth: "fine-tune", updatedAt: expect.any(String), |
| 108 | }); |
| 109 | expect(saved.coding).toBeUndefined(); |
| 110 | expect((await change("survey", { survey: "nope" })).status).toBe(400); |
| 111 | |
| 112 | asUser(otherId); |
| 113 | expect(await (await read("survey")).json()).toEqual({ data: null }); |
| 114 | expect((await change("survey", { survey: null })).status).toBe(200); |
| 115 | |
| 116 | asUser(ownerId); |
| 117 | expect((await (await read("survey")).json()).data).toMatchObject({ role: "researcher" }); |
| 118 | expect((await change("survey", { survey: { ...survey, role: "student" } })).status).toBe(200); |
| 119 | expect((await (await read("survey")).json()).data).toMatchObject({ role: "student" }); |
| 120 | expect((await change("survey", { survey: null })).status).toBe(200); |
| 121 | expect(await (await read("survey")).json()).toEqual({ data: null }); |
| 122 | }); |