Skip to content

Cloudflare Worker integration test suite #215

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
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration/cloudflare-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.wrangler
node_modules
25 changes: 25 additions & 0 deletions integration/cloudflare-worker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Replicate from "replicate";

const replicate = new Replicate();

export default {
async fetch(_request, _env, _ctx) {
const stream = new ReadableStream({
async start(controller) {
for await (const event of replicate.stream(
"replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
{
input: {
text: "Colin CloudFlare",
},
}
)) {
controller.enqueue(`${event}`);
}
controller.close();
},
});

return new Response(stream);
},
};
28 changes: 28 additions & 0 deletions integration/cloudflare-worker/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://developers.cloudflare.com/workers/wrangler/api/#unstable_dev
import { unstable_dev as dev } from "wrangler";
import { test, after, before } from "node:test";
import assert from "node:assert";

let worker;

before(async () => {
worker = await dev("index.js", {
experimental: { disableExperimentalWarning: true },
});
});

after(async () => {
if (!worker) {
// If no worker the before hook failed to run and the process will hang.
process.exit(1);
}
await worker.stop();
});

test("worker streams back a response", { timeout: 1000 }, async (t) => {
const resp = await worker.fetch("/", { signal: t.signal });
const text = await resp.text();

assert.equal(resp.ok, true, "status is 2xx");
assert(text.length > 0, "body.length is greater than 0");
});
Loading