Skip to content

Commit

Permalink
Add simple test to Workers AI binding
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 30, 2023
1 parent d3b405d commit e730d18
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/ai-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
22 changes: 22 additions & 0 deletions fixtures/ai-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "ai-app",
"version": "1.0.1",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"main": "src/index.js",
"scripts": {
"check:type": "tsc",
"test": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"wrangler": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:^"
},
"dependencies": {
"@cloudflare/ai": "^1.0.35"
}
}
12 changes: 12 additions & 0 deletions fixtures/ai-app/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
console.log("startup log");

export default {
async fetch(request, env) {
console.log("request log");

return Response.json({
binding: env.AI,
fetcher: env.AI.fetch.toString(),
});
},
};
31 changes: 31 additions & 0 deletions fixtures/ai-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { resolve } from "path";
import { fetch } from "undici";
import { describe, it, beforeAll, afterAll } from "vitest";
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";

describe("'wrangler dev' correctly renders pages", () => {
let ip: string,
port: number,
stop: (() => Promise<unknown>) | undefined,
getOutput: () => string;

beforeAll(async () => {
({ ip, port, stop, getOutput } = await runWranglerDev(
resolve(__dirname, ".."),
["--local", "--port=0", "--inspector-port=0"]
));
});

afterAll(async () => {
await stop?.();
});

it("ai binding is defined ", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
const content = await response.json();
expect(content).toEqual({
binding: {},
fetcher: "function fetch() { [native code] }",
});
});
});
7 changes: 7 additions & 0 deletions fixtures/ai-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
13 changes: 13 additions & 0 deletions fixtures/ai-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"esModuleInterop": true,
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["node"],
"skipLibCheck": true,
"moduleResolution": "node",
"noEmit": true
},
"include": ["tests", "../../node-types.d.ts"]
}
10 changes: 10 additions & 0 deletions fixtures/ai-app/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
testTimeout: 10_000,
hookTimeout: 10_000,
teardownTimeout: 10_000,
useAtomics: true,
},
});
7 changes: 7 additions & 0 deletions fixtures/ai-app/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "ai-app"
compatibility_date = "2023-11-21"

main = "src/index.js"

[ai]
binding = "AI"

0 comments on commit e730d18

Please sign in to comment.