From b03a38c6e85f71ca010a91164460b4bbafd36171 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 2 Jun 2024 22:01:25 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parse-structured-output.test.ts.snap | 133 ++++++++++++++++++ src/utils.test.ts | 48 ++++--- 2 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 src/__snapshots__/parse-structured-output.test.ts.snap diff --git a/src/__snapshots__/parse-structured-output.test.ts.snap b/src/__snapshots__/parse-structured-output.test.ts.snap new file mode 100644 index 0000000..c32098d --- /dev/null +++ b/src/__snapshots__/parse-structured-output.test.ts.snap @@ -0,0 +1,133 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`parseArrayOutput - handles arrays surrounded by text correctly > should return ["a", "b", "c"] for "Array: ["a", "b", "c"]. That's all!" 1`] = ` +[ + "a", + "b", + "c", +] +`; + +exports[`parseArrayOutput - handles arrays surrounded by text correctly > should return [{"a": 1}, {"b": 2}] for "This is the array [{"a": 1}, {"b": 2}] in the text" 1`] = ` +[ + { + "a": 1, + }, + { + "b": 2, + }, +] +`; + +exports[`parseArrayOutput - handles arrays surrounded by text correctly > should return [1, 2, 3] for "The array is [1,2,3]" 1`] = ` +[ + 1, + 2, + 3, +] +`; + +exports[`parseArrayOutput - handles valid arrays correctly > should return ["a", "b", "c"] for "["a", "b", "c"] 1`] = ` +[ + "a", + "b", + "c", +] +`; + +exports[`parseArrayOutput - handles valid arrays correctly > should return [{"a": 1}, {"b": 2}] for [{"a": 1}, {"b": 2}] 1`] = ` +[ + { + "a": 1, + }, + { + "b": 2, + }, +] +`; + +exports[`parseArrayOutput - handles valid arrays correctly > should return [1, 2, 3] for "[1,2,3]" 1`] = ` +[ + 1, + 2, + 3, +] +`; + +exports[`parseBooleanOutput - handles \`false\` outputs correctly > should return false for "FALSE" 1`] = `false`; + +exports[`parseBooleanOutput - handles \`false\` outputs correctly > should return false for "False" 1`] = `false`; + +exports[`parseBooleanOutput - handles \`false\` outputs correctly > should return false for "false!" 1`] = `false`; + +exports[`parseBooleanOutput - handles \`true\` outputs correctly > should return true for "TRUE" 1`] = `true`; + +exports[`parseBooleanOutput - handles \`true\` outputs correctly > should return true for "True" 1`] = `true`; + +exports[`parseBooleanOutput - handles \`true\` outputs correctly > should return true for "true." 1`] = `true`; + +exports[`parseNumberOutput - handles float outputs correctly > should return -5.5 for " -5.5 " 1`] = `-5.5`; + +exports[`parseNumberOutput - handles float outputs correctly > should return 42.42 for "42.42" 1`] = `42.42`; + +exports[`parseNumberOutput - handles integer outputs correctly > should return -5 for " -5 " 1`] = `-5`; + +exports[`parseNumberOutput - handles integer outputs correctly > should return 42 for "42" 1`] = `42`; + +exports[`parseObjectOutput - handles JSON array of objects > should return first object {"a":1,"b":2} for [{"a":1,"b":2},{"c":3,"d":4}] 1`] = ` +{ + "a": 1, + "b": 2, +} +`; + +exports[`parseObjectOutput - handles objects surrounded by text correctly > should return {"a":1,"b":2,"c":3} for "The object is {"a":1,"b":2,"c":3}" 1`] = ` +{ + "a": 1, + "b": 2, + "c": 3, +} +`; + +exports[`parseObjectOutput - handles objects surrounded by text correctly > should return {"name":"John","age":30,"city":"New York"} for "Object: {"name":"John","age":30,"city":"New York"}. That's all!" 1`] = ` +{ + "age": 30, + "city": "New York", + "name": "John", +} +`; + +exports[`parseObjectOutput - handles valid objects correctly > should return {"a":1,"b":2,"c":3} for {"a":1,"b":2,"c":3} 1`] = ` +{ + "a": 1, + "b": 2, + "c": 3, +} +`; + +exports[`parseObjectOutput - handles valid objects correctly > should return {"name":"John","age":30,"city":"New York"} for {"name":"John","age":30,"city":"New York"} 1`] = ` +{ + "age": 30, + "city": "New York", + "name": "John", +} +`; + +exports[`parseStructuredOutput - handles arrays correctly > should parse and return [1, 2, 3] for "[1, 2, 3]" 1`] = ` +[ + 1, + 2, + 3, +] +`; + +exports[`parseStructuredOutput - handles booleans correctly > should parse and return true for "True" 1`] = `true`; + +exports[`parseStructuredOutput - handles numbers correctly > should parse and return 123.45 for "123.45" 1`] = `123.45`; + +exports[`parseStructuredOutput - handles objects correctly > should parse and return {"a": 1, "b": "two"} for "{"a": 1, "b": "two"}" 1`] = ` +{ + "a": 1, + "b": "two", +} +`; diff --git a/src/utils.test.ts b/src/utils.test.ts index 77e2c35..269d018 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -19,32 +19,36 @@ test('omit', () => { ).toEqual({ a: { b: 'foo' }, d: -1 }) }) -test('throttleKy should rate-limit requests to ky properly', async () => { - // TODO: set timeout +test( + 'throttleKy should rate-limit requests to ky properly', + async () => { + const interval = 1000 + const throttle = pThrottle({ + limit: 1, + interval, + strict: true + }) - const interval = 1000 - const throttle = pThrottle({ - limit: 1, - interval, - strict: true - }) + const ky2 = mockKyInstance(throttleKy(ky, throttle)) - const ky2 = mockKyInstance(throttleKy(ky, throttle)) + const url = 'https://httpbin.org/get' - const url = 'https://httpbin.org/get' + for (let i = 0; i < 10; i++) { + const before = Date.now() + const res = await ky2.get(url) + const after = Date.now() - for (let i = 0; i < 10; i++) { - const before = Date.now() - const res = await ky2.get(url) - const after = Date.now() + const duration = after - before + // console.log(duration, res.status) + expect(res.status).toBe(200) - const duration = after - before - // console.log(duration, res.status) - expect(res.status).toBe(200) - - // leave a bit of wiggle room for the interval - if (i > 0) { - expect(duration >= interval - interval / 5).toBeTruthy() + // leave a bit of wiggle room for the interval + if (i > 0) { + expect(duration >= interval - interval / 5).toBeTruthy() + } } + }, + { + timeout: 60_000 } -}) +)