Skip to content

Commit

Permalink
v4.0.0-beta.7
Browse files Browse the repository at this point in the history
  • Loading branch information
schnerd committed Jul 29, 2023
1 parent 96e5b7f commit 347a227
Show file tree
Hide file tree
Showing 20 changed files with 353 additions and 203 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import OpenAI from 'openai';
const openai = new OpenAI();

async function main() {
const completion = await openai.chat.completions.create({
const stream = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test' }],
stream: true,
Expand Down
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Types:

- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>

Methods:

Expand Down
119 changes: 100 additions & 19 deletions ecosystem-tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ function parseArgs() {
default: false,
description: 'Push projects to live servers',
},
jobs: {
type: 'number',
default: 1,
description: 'number of parallel jobs to run',
},
parallel: {
type: 'boolean',
default: false,
description: 'run all projects in parallel (jobs = # projects)',
},
})
.help().argv;
}
Expand Down Expand Up @@ -157,26 +167,97 @@ async function main() {

const failed: typeof projectNames = [];

for (const project of projectsToRun) {
const fn = projects[project];

await withChdir(path.join(rootDir, 'ecosystem-tests', project), async () => {
console.error('\n');
console.error(banner(project));
console.error('\n');

try {
await fn();
console.error(`✅ - Successfully ran ${project}`);
} catch (err) {
if (err && (err as any).shortMessage) {
console.error((err as any).shortMessage);
} else {
console.error(err);
}
failed.push(project);
let { jobs } = args;
if (args.parallel) jobs = projectsToRun.length;
if (jobs > 1) {
const queue = [...projectsToRun];
const runningProjects = new Set();

const cursorLeft = '\x1B[G';
const eraseLine = '\x1B[2K';

let progressDisplayed = false;
function clearProgress() {
if (progressDisplayed) {
process.stderr.write(cursorLeft + eraseLine);
progressDisplayed = false;
}
});
}
const spinner = ['|', '/', '-', '\\'];

function showProgress() {
clearProgress();
progressDisplayed = true;
const spin = spinner[Math.floor(Date.now() / 500) % spinner.length];
process.stderr.write(
`${spin} Running ${[...runningProjects].join(', ')}`.substring(0, process.stdout.columns - 3) + '...',
);
}

const progressInterval = setInterval(showProgress, process.stdout.isTTY ? 500 : 5000);
showProgress();

await Promise.all(
[...Array(jobs).keys()].map(async () => {
while (queue.length) {
const project = queue.shift();
if (!project) break;
let stdout, stderr;
try {
runningProjects.add(project);
const result = await execa(
'yarn',
[
'tsn',
__filename,
project,
'--skip-pack',
...(args.live ? ['--live'] : []),
...(args.verbose ? ['--verbose'] : []),
...(args.deploy ? ['--deploy'] : []),
...(args.fromNpm ? ['--from-npm'] : []),
],
{ stdio: 'pipe', encoding: 'utf8', maxBuffer: 100 * 1024 * 1024 },
);
({ stdout, stderr } = result);
} catch (error) {
({ stdout, stderr } = error as any);
failed.push(project);
} finally {
runningProjects.delete(project);
}

if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr);
}
}),
);

clearInterval(progressInterval);
clearProgress();
} else {
for (const project of projectsToRun) {
const fn = projects[project];

await withChdir(path.join(rootDir, 'ecosystem-tests', project), async () => {
console.error('\n');
console.error(banner(project));
console.error('\n');

try {
await fn();
console.error(`✅ - Successfully ran ${project}`);
} catch (err) {
if (err && (err as any).shortMessage) {
console.error('❌', (err as any).shortMessage);
} else {
console.error('❌', err);
}
failed.push(project);
}
console.error('\n');
});
}
}

if (failed.length) {
Expand Down
96 changes: 54 additions & 42 deletions ecosystem-tests/cloudflare-worker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import OpenAI from 'openai';
import { uploadWebApiTestCases } from './uploadWebApiTestCases.js';
import { distance } from 'fastest-levenshtein'
import { distance } from 'fastest-levenshtein';

/**
* Welcome to Cloudflare Workers! This is your first worker.
Expand Down Expand Up @@ -35,54 +33,68 @@ type Test = { description: string; handler: () => Promise<void> };

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const client = new OpenAI({ apiKey: env.OPENAI_API_KEY });
try {
console.error('importing openai');
const { default: OpenAI } = await import('openai');
console.error('importing test cases');
const { uploadWebApiTestCases } = await import('./uploadWebApiTestCases.js');
console.error('creating client');
const client = new OpenAI({ apiKey: env.OPENAI_API_KEY });
console.error('created client');

const tests: Test[] = [];
function it(description: string, handler: () => Promise<void>) {
tests.push({ description, handler });
}
function expectEqual(a: any, b: any) {
if (!Object.is(a, b)) {
throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`);
const tests: Test[] = [];
function it(description: string, handler: () => Promise<void>) {
tests.push({ description, handler });
}
}
function expectSimilar(received: string, expected: string, maxDistance: number) {
const receivedDistance = distance(received, expected);
if (receivedDistance < maxDistance) {
return;
function expectEqual(a: any, b: any) {
if (!Object.is(a, b)) {
throw new Error(`expected values to be equal: ${JSON.stringify({ a, b })}`);
}
}
function expectSimilar(received: string, expected: string, maxDistance: number) {
const receivedDistance = distance(received, expected);
if (receivedDistance < maxDistance) {
return;
}

const message = [
`Received: ${JSON.stringify(received)}`,
`Expected: ${JSON.stringify(expected)}`,
`Max distance: ${maxDistance}`,
`Received distance: ${receivedDistance}`,
].join('\n');
const message = [
`Received: ${JSON.stringify(received)}`,
`Expected: ${JSON.stringify(expected)}`,
`Max distance: ${maxDistance}`,
`Received distance: ${receivedDistance}`,
].join('\n');

throw new Error(message);
}
throw new Error(message);
}

uploadWebApiTestCases({
client: client as any,
it,
expectEqual,
expectSimilar,
});
uploadWebApiTestCases({
client: client as any,
it,
expectEqual,
expectSimilar,
});

let allPassed = true;
const results = [];
let allPassed = true;
const results = [];

for (const { description, handler } of tests) {
let result;
try {
result = await handler();
} catch (error) {
allPassed = false;
result = error instanceof Error ? error.stack : String(error);
for (const { description, handler } of tests) {
console.error('running', description);
let result;
try {
result = await handler();
console.error('passed ', description);
} catch (error) {
console.error('failed ', description, error);
allPassed = false;
result = error instanceof Error ? error.stack : String(error);
}
results.push(`${description}\n\n${String(result)}`);
}
results.push(`${description}\n\n${String(result)}`);
}

return new Response(allPassed ? 'Passed!' : results.join('\n\n'));
return new Response(allPassed ? 'Passed!' : results.join('\n\n'));
} catch (error) {
console.error(error instanceof Error ? error.stack : String(error));
return new Response(error instanceof Error ? error.stack : String(error), { status: 500 });
}
},
};
10 changes: 7 additions & 3 deletions ecosystem-tests/cloudflare-worker/tests/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fetch from 'node-fetch';

it('works', async () => {
expect(await (await fetch('http://localhost:8787')).text()).toEqual('Passed!');
}, 30000);
it(
'works',
async () => {
expect(await (await fetch('http://localhost:8787')).text()).toEqual('Passed!');
},
3 * 60000
);
2 changes: 1 addition & 1 deletion ecosystem-tests/deno/main_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals, AssertionError } from 'https://deno.land/std@0.192.0/testing/asserts.ts';
import OpenAI, { toFile } from 'npm:openai@3.3.0';
import { distance } from 'https://deno.land/x/fastest_levenshtein/mod.ts'
import { distance } from 'https://deno.land/x/fastest_levenshtein/mod.ts';

const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
const filename = 'sample-1.mp3';
Expand Down
2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-cjs-dom/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = {
testMatch: ['<rootDir>/tests/*.ts'],
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
verbose: false,
testTimeout: 15000,
testTimeout: 60000,
};
2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-cjs/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = {
testMatch: ['<rootDir>/tests/*.ts'],
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
verbose: false,
testTimeout: 15000,
testTimeout: 60000,
};
2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-esm-dom/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ module.exports = {
testMatch: ['<rootDir>/tests/*.ts'],
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
verbose: false,
testTimeout: 15000,
testTimeout: 60000,
};
2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-esm/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ module.exports = {
testMatch: ['<rootDir>/tests/*.ts'],
watchPathIgnorePatterns: ['<rootDir>/node_modules/'],
verbose: false,
testTimeout: 15000,
testTimeout: 60000,
};
2 changes: 1 addition & 1 deletion ecosystem-tests/ts-browser-webpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function describe(description: string, handler: () => void) {
}
}

function it(description: string, run: () => any, timeout = 15000) {
function it(description: string, run: () => any, timeout = 60000) {
tests.push({ path: [...testPath, description], run, timeout });
}

Expand Down
Loading

0 comments on commit 347a227

Please sign in to comment.