|
1 |
| -import OpenAI from 'openai'; |
2 |
| -import { uploadWebApiTestCases } from './uploadWebApiTestCases.js'; |
3 |
| -import { distance } from 'fastest-levenshtein' |
| 1 | +import { distance } from 'fastest-levenshtein'; |
4 | 2 |
|
5 | 3 | /**
|
6 | 4 | * Welcome to Cloudflare Workers! This is your first worker.
|
@@ -35,54 +33,68 @@ type Test = { description: string; handler: () => Promise<void> };
|
35 | 33 |
|
36 | 34 | export default {
|
37 | 35 | async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
38 |
| - const client = new OpenAI({ apiKey: env.OPENAI_API_KEY }); |
| 36 | + try { |
| 37 | + console.error('importing openai'); |
| 38 | + const { default: OpenAI } = await import('openai'); |
| 39 | + console.error('importing test cases'); |
| 40 | + const { uploadWebApiTestCases } = await import('./uploadWebApiTestCases.js'); |
| 41 | + console.error('creating client'); |
| 42 | + const client = new OpenAI({ apiKey: env.OPENAI_API_KEY }); |
| 43 | + console.error('created client'); |
39 | 44 |
|
40 |
| - const tests: Test[] = []; |
41 |
| - function it(description: string, handler: () => Promise<void>) { |
42 |
| - tests.push({ description, handler }); |
43 |
| - } |
44 |
| - function expectEqual(a: any, b: any) { |
45 |
| - if (!Object.is(a, b)) { |
46 |
| - throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`); |
| 45 | + const tests: Test[] = []; |
| 46 | + function it(description: string, handler: () => Promise<void>) { |
| 47 | + tests.push({ description, handler }); |
47 | 48 | }
|
48 |
| - } |
49 |
| - function expectSimilar(received: string, expected: string, maxDistance: number) { |
50 |
| - const receivedDistance = distance(received, expected); |
51 |
| - if (receivedDistance < maxDistance) { |
52 |
| - return; |
| 49 | + function expectEqual(a: any, b: any) { |
| 50 | + if (!Object.is(a, b)) { |
| 51 | + throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`); |
| 52 | + } |
53 | 53 | }
|
| 54 | + function expectSimilar(received: string, expected: string, maxDistance: number) { |
| 55 | + const receivedDistance = distance(received, expected); |
| 56 | + if (receivedDistance < maxDistance) { |
| 57 | + return; |
| 58 | + } |
54 | 59 |
|
55 |
| - const message = [ |
56 |
| - `Received: ${JSON.stringify(received)}`, |
57 |
| - `Expected: ${JSON.stringify(expected)}`, |
58 |
| - `Max distance: ${maxDistance}`, |
59 |
| - `Received distance: ${receivedDistance}`, |
60 |
| - ].join('\n'); |
| 60 | + const message = [ |
| 61 | + `Received: ${JSON.stringify(received)}`, |
| 62 | + `Expected: ${JSON.stringify(expected)}`, |
| 63 | + `Max distance: ${maxDistance}`, |
| 64 | + `Received distance: ${receivedDistance}`, |
| 65 | + ].join('\n'); |
61 | 66 |
|
62 |
| - throw new Error(message); |
63 |
| - } |
| 67 | + throw new Error(message); |
| 68 | + } |
64 | 69 |
|
65 |
| - uploadWebApiTestCases({ |
66 |
| - client: client as any, |
67 |
| - it, |
68 |
| - expectEqual, |
69 |
| - expectSimilar, |
70 |
| - }); |
| 70 | + uploadWebApiTestCases({ |
| 71 | + client: client as any, |
| 72 | + it, |
| 73 | + expectEqual, |
| 74 | + expectSimilar, |
| 75 | + }); |
71 | 76 |
|
72 |
| - let allPassed = true; |
73 |
| - const results = []; |
| 77 | + let allPassed = true; |
| 78 | + const results = []; |
74 | 79 |
|
75 |
| - for (const { description, handler } of tests) { |
76 |
| - let result; |
77 |
| - try { |
78 |
| - result = await handler(); |
79 |
| - } catch (error) { |
80 |
| - allPassed = false; |
81 |
| - result = error instanceof Error ? error.stack : String(error); |
| 80 | + for (const { description, handler } of tests) { |
| 81 | + console.error('running', description); |
| 82 | + let result; |
| 83 | + try { |
| 84 | + result = await handler(); |
| 85 | + console.error('passed ', description); |
| 86 | + } catch (error) { |
| 87 | + console.error('failed ', description, error); |
| 88 | + allPassed = false; |
| 89 | + result = error instanceof Error ? error.stack : String(error); |
| 90 | + } |
| 91 | + results.push(`${description}\n\n${String(result)}`); |
82 | 92 | }
|
83 |
| - results.push(`${description}\n\n${String(result)}`); |
84 |
| - } |
85 | 93 |
|
86 |
| - return new Response(allPassed ? 'Passed!' : results.join('\n\n')); |
| 94 | + return new Response(allPassed ? 'Passed!' : results.join('\n\n')); |
| 95 | + } catch (error) { |
| 96 | + console.error(error instanceof Error ? error.stack : String(error)); |
| 97 | + return new Response(error instanceof Error ? error.stack : String(error), { status: 500 }); |
| 98 | + } |
87 | 99 | },
|
88 | 100 | };
|
0 commit comments