-
Notifications
You must be signed in to change notification settings - Fork 34
Add fake payment API #15
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
Open
maxrross
wants to merge
2
commits into
tscircuit:main
Choose a base branch
from
maxrross:codex/add-fake-payment-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Binary file not shown.
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
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,17 @@ | ||
| import { paymentSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: z.object({ | ||
| payment_id: z.string().min(1), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payment: paymentSchema.nullable(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "cancelled") ?? null | ||
| return ctx.json({ payment }) | ||
| }) |
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,17 @@ | ||
| import { paymentSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: z.object({ | ||
| payment_id: z.string().min(1), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payment: paymentSchema.nullable(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "completed") ?? null | ||
| return ctx.json({ payment }) | ||
| }) | ||
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,17 @@ | ||
| import { paymentSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: z.object({ | ||
| payment_id: z.string().min(1), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payment: paymentSchema.nullable(), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const { payment_id } = await req.json() | ||
| const payment = ctx.db.updatePaymentStatus(payment_id, "failed") ?? null | ||
| return ctx.json({ payment }) | ||
| }) |
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,18 @@ | ||
| import { paymentSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| queryParams: z.object({ | ||
| payment_id: z.string().min(1), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payment: paymentSchema.nullable(), | ||
| }), | ||
| })((req, ctx) => { | ||
| const { payment_id } = req.query | ||
| const payment = | ||
| ctx.db.payments.find((payment) => payment.payment_id === payment_id) ?? null | ||
| return ctx.json({ payment }) | ||
| }) |
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,25 @@ | ||
| import { paymentSchema, paymentStatusSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| queryParams: z.object({ | ||
| recipient: z.string().optional(), | ||
| status: paymentStatusSchema.optional(), | ||
| repository: z.string().optional(), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payments: z.array(paymentSchema), | ||
| }), | ||
| })((req, ctx) => { | ||
| const { recipient, status, repository } = req.query | ||
| const payments = ctx.db.payments.filter((payment) => { | ||
| return ( | ||
| (!recipient || payment.recipient === recipient) && | ||
| (!status || payment.status === status) && | ||
| (!repository || payment.repository === repository) | ||
| ) | ||
| }) | ||
| return ctx.json({ payments }) | ||
| }) |
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,23 @@ | ||
| import { paymentSchema } from "lib/db/schema" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["POST"], | ||
| jsonBody: z.object({ | ||
| recipient: z.string().min(1), | ||
| amount: z.number().positive(), | ||
| currency: z.string().min(1).default("USD"), | ||
| bounty_id: z.string().min(1).optional(), | ||
| issue_number: z.number().int().positive().optional(), | ||
| repository: z.string().min(1).optional(), | ||
| idempotency_key: z.string().min(1).optional(), | ||
| }), | ||
| jsonResponse: z.object({ | ||
| payment: paymentSchema, | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const body = await req.json() | ||
| const payment = ctx.db.sendPayment(body) | ||
| return ctx.json({ payment }) | ||
| }) |
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,106 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { join } from "node:path" | ||
| import { Request as EdgeRuntimeRequest } from "@edge-runtime/primitives" | ||
| import { getTestServer } from "tests/fixtures/get-test-server" | ||
| import { createWinterSpecBundleFromDir } from "winterspec/adapters/node" | ||
|
|
||
| test("sends and completes a fake payment", async () => { | ||
| const { axios } = await getTestServer() | ||
|
|
||
| const { data: sendData } = await axios.post("/payments/send", { | ||
| recipient: "contributor@example.com", | ||
| amount: 10, | ||
| currency: "USD", | ||
| bounty_id: "fake-algora-1", | ||
| issue_number: 1, | ||
| repository: "tscircuit/fake-algora", | ||
| }) | ||
|
|
||
| expect(sendData.payment.payment_id).toBe("0") | ||
| expect(sendData.payment.status).toBe("pending") | ||
|
|
||
| const { data: completeData } = await axios.post("/payments/complete", { | ||
| payment_id: sendData.payment.payment_id, | ||
| }) | ||
|
|
||
| expect(completeData.payment.status).toBe("completed") | ||
|
|
||
| const { data: getData } = await axios.get("/payments/get", { | ||
| params: { payment_id: sendData.payment.payment_id }, | ||
| }) | ||
|
|
||
| expect(getData.payment.status).toBe("completed") | ||
| }) | ||
|
|
||
| test("does not duplicate payments when an idempotency key is reused", async () => { | ||
| const { axios } = await getTestServer() | ||
|
|
||
| const payload = { | ||
| recipient: "contributor@example.com", | ||
| amount: 20, | ||
| currency: "USD", | ||
| idempotency_key: "retry-safe-key", | ||
| } | ||
|
|
||
| const { data: firstSend } = await axios.post("/payments/send", payload) | ||
| const { data: secondSend } = await axios.post("/payments/send", payload) | ||
| const { data: listData } = await axios.get("/payments/list") | ||
|
|
||
| expect(firstSend.payment.payment_id).toBe(secondSend.payment.payment_id) | ||
| expect(listData.payments).toHaveLength(1) | ||
| }) | ||
|
|
||
| test("filters listed payments", async () => { | ||
| const { axios } = await getTestServer() | ||
|
|
||
| await axios.post("/payments/send", { | ||
| recipient: "first@example.com", | ||
| amount: 10, | ||
| repository: "tscircuit/fake-algora", | ||
| }) | ||
| await axios.post("/payments/send", { | ||
| recipient: "second@example.com", | ||
| amount: 15, | ||
| repository: "tscircuit/other", | ||
| }) | ||
|
|
||
| const { data } = await axios.get("/payments/list", { | ||
| params: { repository: "tscircuit/fake-algora" }, | ||
| }) | ||
|
|
||
| expect(data.payments).toHaveLength(1) | ||
| expect(data.payments[0].recipient).toBe("first@example.com") | ||
| }) | ||
|
|
||
| test("shares payment records when no db middleware is injected", async () => { | ||
| const winterspecBundle = await createWinterSpecBundleFromDir( | ||
| join(import.meta.dir, "../../../routes"), | ||
| ) | ||
| const idempotencyKey = `production-path-${Math.random().toString(36).slice(2)}` | ||
|
|
||
| const sendResponse = await winterspecBundle.makeRequest( | ||
| new EdgeRuntimeRequest("http://localhost/payments/send", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ | ||
| recipient: "production@example.com", | ||
| amount: 25, | ||
| idempotency_key: idempotencyKey, | ||
| }), | ||
| }) as any, | ||
| ) | ||
| const sendData = await sendResponse.json() | ||
|
|
||
| const completeResponse = await winterspecBundle.makeRequest( | ||
| new EdgeRuntimeRequest("http://localhost/payments/complete", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ | ||
| payment_id: sendData.payment.payment_id, | ||
| }), | ||
| }) as any, | ||
| ) | ||
| const completeData = await completeResponse.json() | ||
|
|
||
| expect(completeData.payment.status).toBe("completed") | ||
| }) |
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.
In the real Next/Winterspec handler (
pages/api/[[...route]].ts), no shared DB middleware is injected, sowithDbcreates a new in-memory store wheneverctx.dbis absent; onlytests/fixtures/start-server.tssupplies a process-wide DB. Because this endpoint looks up a payment created by a previous/payments/sendHTTP request, production calls to/payments/completeafter sending will returnnull(and/payments/getor/payments/listwill similarly see an empty store), making the new payment lifecycle unusable outside the test fixture.Useful? React with 👍 / 👎.
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.
Fixed in ff80a6e.
withDbnow falls back to a process-wide shared store when production does not injectctx.db, while tests can still inject isolated DBs. I also added a regression test that sends and completes a payment through a Winterspec bundle without any injected DB middleware, covering the lifecycle path from the review.