-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add deployed cf and nextjs-12 tests
- Loading branch information
Showing
12 changed files
with
202 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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,65 @@ | ||
|
||
import { Client } from "@upstash/qstash" | ||
import { Redis } from "@upstash/redis" | ||
import { serve } from "@upstash/workflow/nextjs" | ||
import { describe, test, expect } from "bun:test" | ||
|
||
const qstashClient = new Client({ | ||
baseUrl: "https://workflow-tests.requestcatcher.com/", | ||
token: "mock" | ||
}) | ||
|
||
// @ts-expect-error mocking publishJSON | ||
qstashClient.publishJSON = async () => { | ||
return { messageId: "msgId" } | ||
} | ||
|
||
const { POST: serveHandler } = serve( | ||
async (context) => { | ||
await context.sleep("sleeping", 10) | ||
}, { | ||
qstashClient, | ||
receiver: undefined | ||
} | ||
) | ||
|
||
describe("cloudflare workers tests", () => { | ||
test("should send first invocation", async () => { | ||
const request = new Request("https://workflow-tests.requestcatcher.com/") | ||
const response = await serveHandler(request) | ||
|
||
// it should send a request, but get failed to parse error because | ||
// request catcher returns string | ||
expect(response.status).toBe(200) | ||
const result = await response.json() as { workflowRunId: string } | ||
expect(result.workflowRunId).toBeTruthy() | ||
}) | ||
|
||
if (process.env.DEPLOYMENT_URL) { | ||
test("should run workflow successfully", async () => { | ||
const redis = Redis.fromEnv() | ||
const client = new Client({ token: process.env.QSTASH_TOKEN! }) | ||
|
||
const secret = "secret" + Math.floor(Math.random() * 10000).toString() | ||
await client.publishJSON({ | ||
url: `${process.env.DEPLOYMENT_URL}/ci`, | ||
body: { text: "hello world!" }, | ||
method: "POST", | ||
headers: { | ||
"Content-type": "text/plain", | ||
"secret-header": secret | ||
} | ||
}) | ||
|
||
await new Promise(r => setTimeout(r, 3000)); | ||
|
||
const result = await redis.get<string>(`ci-cf-ran-${secret}`) | ||
|
||
if (result !== secret) { | ||
throw new Error("Cloudflare workflow didn't run") | ||
} | ||
}) | ||
} else { | ||
console.log("skipping workflow run tests because DEPLOYMENT_URL is not set"); | ||
} | ||
}) |
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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,49 @@ | ||
/** | ||
* this endpoint is for the CI of @upstash/workflow. | ||
* | ||
* refer to workflow.js for a simpler example. | ||
*/ | ||
import { servePagesRouter } from "@upstash/workflow/nextjs"; | ||
import { Redis } from "@upstash/redis" | ||
|
||
const someWork = (input) => { | ||
return `processed '${JSON.stringify(input)}'` | ||
} | ||
|
||
const baseUrl = process.env.VERCEL_URL | ||
? `https://${process.env.VERCEL_URL}` | ||
: process.env.UPSTASH_WORKFLOW_URL | ||
? process.env.UPSTASH_WORKFLOW_URL | ||
: "http://localhost:3001" | ||
|
||
const endpointUrl = `${baseUrl}/api/ci` | ||
|
||
const redis = Redis.fromEnv() | ||
|
||
const { handler } = servePagesRouter( | ||
async (context) => { | ||
const input = context.requestPayload | ||
const result1 = await context.run("step1", async () => { | ||
const output = someWork(input) | ||
console.log("step 1 input", input, "output", output) | ||
return output | ||
}); | ||
|
||
await context.sleep("sleep", 1); | ||
|
||
const secret = context.headers.get("secret-header") | ||
if (!secret) { | ||
console.error("secret not found"); | ||
throw new Error("secret not found. can't end the CI workflow") | ||
} else { | ||
console.log("saving secret to redis"); | ||
await redis.set(`ci-cf-ran-${secret}`, secret) | ||
} | ||
}, | ||
{ | ||
retries: 0, | ||
url: endpointUrl | ||
} | ||
) | ||
|
||
export default handler |
This file contains 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 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