Skip to content

Commit ea9c7af

Browse files
committed
add a couple tests
1 parent 7602b08 commit ea9c7af

File tree

8 files changed

+108
-25
lines changed

8 files changed

+108
-25
lines changed

.github/workflows/checks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ jobs:
3232

3333
- name: ESLint
3434
run: yarn lint
35+
36+
- name: Test
37+
run: yarn test

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jest": "^29.3.1",
2121
"openai": "^3.1.0",
2222
"prettier": "^2.8.1",
23+
"ts-jest": "^29.0.3",
2324
"ts-node": "^10.9.1",
2425
"tsconfig-paths": "^4.1.1",
2526
"tsconfig-paths-jest": "^0.0.1",
@@ -36,7 +37,8 @@
3637
"lint": "yarn lint:ts --cache",
3738
"lint:ci": "yarn lint:ts",
3839
"lint:ts": "eslint . --ext .js,.ts,.json",
39-
"prettier:all": "prettier --write \"**/*.+(js|jsx|ts|tsx|json|css|html)\""
40+
"prettier:all": "prettier --write \"**/*.+(js|jsx|ts|tsx|json|css|html)\"",
41+
"test": "jest --config test/jest.config.js"
4042
},
4143
"dependencies": {
4244
"@upstash/redis": "^1.18.1"

src/cache.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getCacheKey } from './cache';
2+
import { objectHash } from './utils';
3+
4+
jest.mock('./utils.ts');
5+
6+
describe('cache', () => {
7+
describe('getCacheKey', () => {
8+
beforeEach(() => {
9+
(objectHash as jest.Mock).mockReturnValue('unique-sha-hash');
10+
});
11+
12+
it('returns a hash uniquely representing the params', async () => {
13+
const params = {
14+
method: 'POST',
15+
path: '/v1/completions',
16+
authHeader: 'api-key',
17+
body: '{ "ok": true }',
18+
};
19+
const result = await getCacheKey(params);
20+
expect(objectHash).toHaveBeenCalledWith(params);
21+
expect(result).toEqual('unique-sha-hash');
22+
});
23+
24+
it('removes null or empty values', async () => {
25+
const params = {
26+
method: 'POST',
27+
path: '/v1/completions',
28+
authHeader: null,
29+
body: '',
30+
};
31+
const result = await getCacheKey(params);
32+
expect(objectHash).toHaveBeenCalledWith({
33+
method: 'POST',
34+
path: '/v1/completions',
35+
});
36+
expect(result).toEqual('unique-sha-hash');
37+
});
38+
});
39+
});

src/cache.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import { Redis } from '@upstash/redis/cloudflare';
22
import { Env } from './env';
3-
import { getHeadersAsObject } from './utils';
4-
5-
export const objectHash = async (obj: Record<string, any>): Promise<string> => {
6-
const stringifiedObj = JSON.stringify(obj);
7-
const arrayBuffer = await crypto.subtle.digest(
8-
{
9-
name: 'SHA-256',
10-
},
11-
new TextEncoder().encode(stringifiedObj),
12-
);
13-
// https://github.com/cmackenzie1/holster/tree/main/workers/hash
14-
return [...new Uint8Array(arrayBuffer)].map((x) => x.toString(16).padStart(2, '0')).join('');
15-
};
3+
import { getHeadersAsObject, objectHash } from './utils';
164

175
interface GetCacheKeyProps {
186
method: string;

src/utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
// Return `Headers` as an Object:
12
type HeadersObject = Record<string, string>;
2-
33
export const getHeadersAsObject = (headers: Headers): HeadersObject => {
44
const headersObject: HeadersObject = {};
55
for (const [key, value] of headers.entries()) {
66
headersObject[key] = value;
77
}
88
return headersObject;
99
};
10+
11+
// Return a sha256 hash for a given Object:
12+
export const objectHash = async (obj: Record<string, any>): Promise<string> => {
13+
const stringifiedObj = JSON.stringify(obj);
14+
const arrayBuffer = await crypto.subtle.digest(
15+
{
16+
name: 'SHA-256',
17+
},
18+
new TextEncoder().encode(stringifiedObj),
19+
);
20+
// https://github.com/cmackenzie1/holster/tree/main/workers/hash
21+
return [...new Uint8Array(arrayBuffer)].map((x) => x.toString(16).padStart(2, '0')).join('');
22+
};

test/jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
rootDir: '../',
3+
transform: {
4+
'\\.ts$': 'ts-jest',
5+
},
6+
clearMocks: true,
7+
testEnvironment: 'node',
8+
setupFilesAfterEnv: ['./test/jest.setup.ts'],
9+
};

test/jest.setup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
jest.setTimeout(10000);
2+
3+
export {};

yarn.lock

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,13 @@ browserslist@^4.21.3:
13031303
node-releases "^2.0.6"
13041304
update-browserslist-db "^1.0.9"
13051305

1306+
1307+
version "0.2.6"
1308+
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
1309+
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
1310+
dependencies:
1311+
fast-json-stable-stringify "2.x"
1312+
13061313
13071314
version "2.1.1"
13081315
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.1.tgz#e6787da20ece9d07998533cfd9de6f5c38f4bc05"
@@ -2101,7 +2108,7 @@ fast-glob@^3.2.11, fast-glob@^3.2.9:
21012108
merge2 "^1.3.0"
21022109
micromatch "^4.0.4"
21032110

2104-
fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
2111+
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
21052112
version "2.1.0"
21062113
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
21072114
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
@@ -2962,7 +2969,7 @@ jest-snapshot@^29.3.1:
29622969
pretty-format "^29.3.1"
29632970
semver "^7.3.5"
29642971

2965-
jest-util@^29.3.1:
2972+
jest-util@^29.0.0, jest-util@^29.3.1:
29662973
version "29.3.1"
29672974
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-29.3.1.tgz#1dda51e378bbcb7e3bc9d8ab651445591ed373e1"
29682975
integrity sha512-7YOVZaiX7RJLv76ZfHt4nbNEzzTRiMW/IiOG7ZOKmTXmoGBxUDefgMAxQubu6WPVqP5zSzAdZG0FfLcC7HOIFQ==
@@ -3124,6 +3131,11 @@ locate-path@^6.0.0:
31243131
dependencies:
31253132
p-locate "^5.0.0"
31263133

3134+
3135+
version "4.1.2"
3136+
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
3137+
integrity sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==
3138+
31273139
lodash.merge@^4.6.2:
31283140
version "4.6.2"
31293141
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
@@ -3155,7 +3167,7 @@ make-dir@^3.0.0:
31553167
dependencies:
31563168
semver "^6.0.0"
31573169

3158-
make-error@^1.1.1:
3170+
make-error@1.x, make-error@^1.1.1:
31593171
version "1.3.6"
31603172
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
31613173
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
@@ -3697,18 +3709,18 @@ semiver@^1.1.0:
36973709
resolved "https://registry.yarnpkg.com/semiver/-/semiver-1.1.0.tgz#9c97fb02c21c7ce4fcf1b73e2c7a24324bdddd5f"
36983710
integrity sha512-QNI2ChmuioGC1/xjyYwyZYADILWyW6AmS1UH6gDj/SFUUUS4MBAWs/7mxnkRPc/F4iHezDP+O8t0dO8WHiEOdg==
36993711

3700-
semver@^6.0.0, semver@^6.3.0:
3701-
version "6.3.0"
3702-
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
3703-
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
3704-
3705-
semver@^7.0.0, semver@^7.3.5, semver@^7.3.7:
3712+
[email protected], semver@^7.0.0, semver@^7.3.5, semver@^7.3.7:
37063713
version "7.3.8"
37073714
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798"
37083715
integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==
37093716
dependencies:
37103717
lru-cache "^6.0.0"
37113718

3719+
semver@^6.0.0, semver@^6.3.0:
3720+
version "6.3.0"
3721+
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
3722+
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
3723+
37123724
set-cookie-parser@^2.4.8:
37133725
version "2.5.1"
37143726
resolved "https://registry.yarnpkg.com/set-cookie-parser/-/set-cookie-parser-2.5.1.tgz#ddd3e9a566b0e8e0862aca974a6ac0e01349430b"
@@ -3958,6 +3970,20 @@ tr46@~0.0.3:
39583970
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
39593971
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==
39603972

3973+
ts-jest@^29.0.3:
3974+
version "29.0.3"
3975+
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.0.3.tgz#63ea93c5401ab73595440733cefdba31fcf9cb77"
3976+
integrity sha512-Ibygvmuyq1qp/z3yTh9QTwVVAbFdDy/+4BtIQR2sp6baF2SJU/8CKK/hhnGIDY2L90Az2jIqTwZPnN2p+BweiQ==
3977+
dependencies:
3978+
bs-logger "0.x"
3979+
fast-json-stable-stringify "2.x"
3980+
jest-util "^29.0.0"
3981+
json5 "^2.2.1"
3982+
lodash.memoize "4.x"
3983+
make-error "1.x"
3984+
semver "7.x"
3985+
yargs-parser "^21.0.1"
3986+
39613987
ts-node@^10.9.1:
39623988
version "10.9.1"
39633989
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"
@@ -4245,7 +4271,7 @@ yallist@^4.0.0:
42454271
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
42464272
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
42474273

4248-
yargs-parser@^21.1.1:
4274+
yargs-parser@^21.0.1, yargs-parser@^21.1.1:
42494275
version "21.1.1"
42504276
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
42514277
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

0 commit comments

Comments
 (0)