-
Notifications
You must be signed in to change notification settings - Fork 6
fix: prevent startup crash for new users #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thisisouvik
merged 1 commit into
thisisouvik:main
from
odarome132:fix/startup-null-pointer-new-users
Aug 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from "vitest"; | ||
| import Fastify, { FastifyInstance } from "fastify"; | ||
| import accountRoutes from "../routes/account"; | ||
|
|
||
| // ── DB mock ────────────────────────────────────────────────────────────────── | ||
| // The account route resolves its queries through getDb() (neon). We stub the | ||
| // module so each test can script exactly which rows the route "sees", which is | ||
| // what lets us exercise the missing-user path that previously crashed. | ||
| const { mockSql } = vi.hoisted(() => ({ mockSql: vi.fn() })); | ||
|
|
||
| vi.mock("../lib/db", () => ({ | ||
| getDb: () => mockSql, | ||
| })); | ||
|
|
||
| const USER_ID = "11111111-1111-4111-8111-111111111111"; | ||
| const PUBLIC_KEY = "GCWVJI2QBJQXNJIOU5PAZEAHQFNZGZMQFAF36C2YSF4X4YLQTISN3BQR"; | ||
|
|
||
| // A freshly-registered user row (no rules, no transactions, no limits). | ||
| const newUserRow = { | ||
| id: USER_ID, | ||
| publicKey: PUBLIC_KEY, | ||
| dailyLimit: null, | ||
| weeklyLimit: null, | ||
| plan: null, | ||
| createdAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| }; | ||
|
|
||
| describe("AutoPilot Account Route API Tests", () => { | ||
| let server: FastifyInstance; | ||
|
|
||
| beforeAll(async () => { | ||
| server = Fastify(); | ||
|
|
||
| // Register basic plugins needed by routes (mirrors tests/api.test.ts) | ||
| await server.register(import("@fastify/jwt"), { | ||
| secret: "test-secret", | ||
| cookie: { | ||
| cookieName: "session", | ||
| signed: false, | ||
| }, | ||
| }); | ||
| await server.register(import("@fastify/cookie")); | ||
|
|
||
| server.register(accountRoutes, { prefix: "/api/account" }); | ||
|
|
||
| await server.ready(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await server.close(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| mockSql.mockReset(); | ||
| }); | ||
|
|
||
| it("returns 401 when unauthenticated (missing session cookie)", async () => { | ||
| const response = await server.inject({ | ||
| method: "GET", | ||
| url: "/api/account", | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(401); | ||
| expect(JSON.parse(response.payload)).toHaveProperty("error"); | ||
| }); | ||
|
|
||
| it("returns 401 on PATCH when unauthenticated", async () => { | ||
| const response = await server.inject({ | ||
| method: "PATCH", | ||
| url: "/api/account", | ||
| payload: { dailyLimit: 50 }, | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(401); | ||
| expect(JSON.parse(response.payload)).toHaveProperty("error"); | ||
| }); | ||
|
|
||
| it("returns account details for a newly registered user (empty rules + transactions)", async () => { | ||
| const token = server.jwt.sign({ id: USER_ID, publicKey: PUBLIC_KEY }); | ||
|
|
||
| // Script the three parallel queries: user row, active-rule count, transactions | ||
| mockSql | ||
| .mockResolvedValueOnce([newUserRow]) | ||
| .mockResolvedValueOnce([{ count: "0" }]) | ||
| .mockResolvedValueOnce([]); | ||
|
|
||
| const response = await server.inject({ | ||
| method: "GET", | ||
| url: "/api/account", | ||
| cookies: { session: token }, | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(200); | ||
| const body = JSON.parse(response.payload); | ||
| expect(body.publicKey).toBe(PUBLIC_KEY); | ||
| expect(body.dailyLimit).toBeNull(); | ||
| expect(body.weeklyLimit).toBeNull(); | ||
| expect(body.plan).toBe("free"); | ||
| expect(body.activeRules).toBe(0); | ||
| expect(body.transactions).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns 401 instead of crashing when the session user has no DB row (null-pointer regression)", async () => { | ||
| const token = server.jwt.sign({ id: USER_ID, publicKey: PUBLIC_KEY }); | ||
|
|
||
| // Simulate a valid JWT whose user no longer exists in the DB — this is the | ||
| // path that used to throw on `userRows[0].publicKey` (500 / app crash). | ||
| mockSql | ||
| .mockResolvedValueOnce([]) // user query → no rows | ||
| .mockResolvedValueOnce([{ count: "0" }]) | ||
| .mockResolvedValueOnce([]); | ||
|
|
||
| const response = await server.inject({ | ||
| method: "GET", | ||
| url: "/api/account", | ||
| cookies: { session: token }, | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(401); | ||
| expect(JSON.parse(response.payload).error).toMatch(/User not found/i); | ||
| }); | ||
|
|
||
| it("returns the user's daily limit, plan and active-rule count when populated", async () => { | ||
| const token = server.jwt.sign({ id: USER_ID, publicKey: PUBLIC_KEY }); | ||
|
|
||
| mockSql | ||
| .mockResolvedValueOnce([{ ...newUserRow, dailyLimit: "10.5", weeklyLimit: "50", plan: "premium" }]) | ||
| .mockResolvedValueOnce([{ count: "2" }]) | ||
| .mockResolvedValueOnce([ | ||
| { id: "tx-1", type: "save", amount: 1.25, memo: null, txHash: "abc", createdAt: new Date().toISOString() }, | ||
| ]); | ||
|
|
||
| const response = await server.inject({ | ||
| method: "GET", | ||
| url: "/api/account", | ||
| cookies: { session: token }, | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(200); | ||
| const body = JSON.parse(response.payload); | ||
| expect(body.dailyLimit).toBe("10.5"); | ||
| expect(body.weeklyLimit).toBe("50"); | ||
| expect(body.plan).toBe("premium"); | ||
| expect(body.activeRules).toBe(2); | ||
| expect(body.transactions).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("updates user limits via PATCH when authenticated", async () => { | ||
| const token = server.jwt.sign({ id: USER_ID, publicKey: PUBLIC_KEY }); | ||
|
|
||
| mockSql.mockResolvedValueOnce([]); // UPDATE ... (single query, result ignored) | ||
|
|
||
| const response = await server.inject({ | ||
| method: "PATCH", | ||
| url: "/api/account", | ||
| cookies: { session: token }, | ||
| payload: { dailyLimit: 25, weeklyLimit: 100 }, | ||
| }); | ||
|
|
||
| expect(response.statusCode).toBe(200); | ||
| expect(JSON.parse(response.payload)).toEqual({ success: true }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add error message for not sucessful auth