From e730d18affa5c80a017d4c22ca830ab4f277103f Mon Sep 17 00:00:00 2001 From: Gabriel Massadas Date: Thu, 30 Nov 2023 15:58:00 +0000 Subject: [PATCH] Add simple test to Workers AI binding --- fixtures/ai-app/.gitignore | 1 + fixtures/ai-app/package.json | 22 ++++++++++++++++++++ fixtures/ai-app/src/index.js | 12 +++++++++++ fixtures/ai-app/tests/index.test.ts | 31 +++++++++++++++++++++++++++++ fixtures/ai-app/tests/tsconfig.json | 7 +++++++ fixtures/ai-app/tsconfig.json | 13 ++++++++++++ fixtures/ai-app/vitest.config.ts | 10 ++++++++++ fixtures/ai-app/wrangler.toml | 7 +++++++ 8 files changed, 103 insertions(+) create mode 100644 fixtures/ai-app/.gitignore create mode 100644 fixtures/ai-app/package.json create mode 100644 fixtures/ai-app/src/index.js create mode 100644 fixtures/ai-app/tests/index.test.ts create mode 100644 fixtures/ai-app/tests/tsconfig.json create mode 100644 fixtures/ai-app/tsconfig.json create mode 100644 fixtures/ai-app/vitest.config.ts create mode 100644 fixtures/ai-app/wrangler.toml diff --git a/fixtures/ai-app/.gitignore b/fixtures/ai-app/.gitignore new file mode 100644 index 000000000000..1521c8b7652b --- /dev/null +++ b/fixtures/ai-app/.gitignore @@ -0,0 +1 @@ +dist diff --git a/fixtures/ai-app/package.json b/fixtures/ai-app/package.json new file mode 100644 index 000000000000..56abd6d6290b --- /dev/null +++ b/fixtures/ai-app/package.json @@ -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" + } +} diff --git a/fixtures/ai-app/src/index.js b/fixtures/ai-app/src/index.js new file mode 100644 index 000000000000..269f4491906f --- /dev/null +++ b/fixtures/ai-app/src/index.js @@ -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(), + }); + }, +}; diff --git a/fixtures/ai-app/tests/index.test.ts b/fixtures/ai-app/tests/index.test.ts new file mode 100644 index 000000000000..472bd2c2790f --- /dev/null +++ b/fixtures/ai-app/tests/index.test.ts @@ -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) | 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] }", + }); + }); +}); diff --git a/fixtures/ai-app/tests/tsconfig.json b/fixtures/ai-app/tests/tsconfig.json new file mode 100644 index 000000000000..d2ce7f144694 --- /dev/null +++ b/fixtures/ai-app/tests/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "@cloudflare/workers-tsconfig/tsconfig.json", + "compilerOptions": { + "types": ["node"] + }, + "include": ["**/*.ts", "../../../node-types.d.ts"] +} diff --git a/fixtures/ai-app/tsconfig.json b/fixtures/ai-app/tsconfig.json new file mode 100644 index 000000000000..b901134e4e79 --- /dev/null +++ b/fixtures/ai-app/tsconfig.json @@ -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"] +} diff --git a/fixtures/ai-app/vitest.config.ts b/fixtures/ai-app/vitest.config.ts new file mode 100644 index 000000000000..ed02453b1c87 --- /dev/null +++ b/fixtures/ai-app/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + testTimeout: 10_000, + hookTimeout: 10_000, + teardownTimeout: 10_000, + useAtomics: true, + }, +}); diff --git a/fixtures/ai-app/wrangler.toml b/fixtures/ai-app/wrangler.toml new file mode 100644 index 000000000000..841585819660 --- /dev/null +++ b/fixtures/ai-app/wrangler.toml @@ -0,0 +1,7 @@ +name = "ai-app" +compatibility_date = "2023-11-21" + +main = "src/index.js" + +[ai] +binding = "AI"