diff --git a/data/get-abbreviated-packument.db.gz b/data/get-abbreviated-packument.db.gz deleted file mode 100644 index bcff73a..0000000 Binary files a/data/get-abbreviated-packument.db.gz and /dev/null differ diff --git a/data/get-package-versions-downloads.db.gz b/data/get-package-versions-downloads.db.gz deleted file mode 100644 index d9510e1..0000000 Binary files a/data/get-package-versions-downloads.db.gz and /dev/null differ diff --git a/data/get-packument.db.gz b/data/get-packument.db.gz deleted file mode 100644 index 09042cb..0000000 Binary files a/data/get-packument.db.gz and /dev/null differ diff --git a/data/get-registry-metadata.db.gz b/data/get-registry-metadata.db.gz deleted file mode 100644 index 7f4e604..0000000 Binary files a/data/get-registry-metadata.db.gz and /dev/null differ diff --git a/data/get-registry-signing-keys.db.gz b/data/get-registry-signing-keys.db.gz deleted file mode 100644 index 7dda6a2..0000000 Binary files a/data/get-registry-signing-keys.db.gz and /dev/null differ diff --git a/data/search-packages.db.gz b/data/search-packages.db.gz deleted file mode 100644 index d23bc56..0000000 Binary files a/data/search-packages.db.gz and /dev/null differ diff --git a/data/test/get-abbreviated-packument.json.gz b/data/test/get-abbreviated-packument.json.gz new file mode 100644 index 0000000..a073091 Binary files /dev/null and b/data/test/get-abbreviated-packument.json.gz differ diff --git a/data/get-bulk-daily-package-downloads.db.gz b/data/test/get-bulk-daily-package-downloads.json.gz similarity index 79% rename from data/get-bulk-daily-package-downloads.db.gz rename to data/test/get-bulk-daily-package-downloads.json.gz index 8d9fe0c..116bf0a 100644 Binary files a/data/get-bulk-daily-package-downloads.db.gz and b/data/test/get-bulk-daily-package-downloads.json.gz differ diff --git a/data/get-bulk-package-downloads.db.gz b/data/test/get-bulk-package-downloads.json.gz similarity index 68% rename from data/get-bulk-package-downloads.db.gz rename to data/test/get-bulk-package-downloads.json.gz index f5e13c5..8bec7f6 100644 Binary files a/data/get-bulk-package-downloads.db.gz and b/data/test/get-bulk-package-downloads.json.gz differ diff --git a/data/get-daily-package-downloads.db.gz b/data/test/get-daily-package-downloads.json.gz similarity index 76% rename from data/get-daily-package-downloads.db.gz rename to data/test/get-daily-package-downloads.json.gz index d7c8d45..04e08b2 100644 Binary files a/data/get-daily-package-downloads.db.gz and b/data/test/get-daily-package-downloads.json.gz differ diff --git a/data/get-daily-registry-downloads.db.gz b/data/test/get-daily-registry-downloads.json.gz similarity index 92% rename from data/get-daily-registry-downloads.db.gz rename to data/test/get-daily-registry-downloads.json.gz index 915a8d7..d43f21c 100644 Binary files a/data/get-daily-registry-downloads.db.gz and b/data/test/get-daily-registry-downloads.json.gz differ diff --git a/data/get-package-downloads.db.gz b/data/test/get-package-downloads.json.gz similarity index 60% rename from data/get-package-downloads.db.gz rename to data/test/get-package-downloads.json.gz index acac2ad..9e56769 100644 Binary files a/data/get-package-downloads.db.gz and b/data/test/get-package-downloads.json.gz differ diff --git a/data/get-package-manifest.db.gz b/data/test/get-package-manifest.json.gz similarity index 96% rename from data/get-package-manifest.db.gz rename to data/test/get-package-manifest.json.gz index aa51219..3420432 100644 Binary files a/data/get-package-manifest.db.gz and b/data/test/get-package-manifest.json.gz differ diff --git a/data/test/get-package-versions-downloads.json.gz b/data/test/get-package-versions-downloads.json.gz new file mode 100644 index 0000000..a8a20d0 Binary files /dev/null and b/data/test/get-package-versions-downloads.json.gz differ diff --git a/data/test/get-packument.json.gz b/data/test/get-packument.json.gz new file mode 100644 index 0000000..9038ff2 Binary files /dev/null and b/data/test/get-packument.json.gz differ diff --git a/data/get-registry-downloads.db.gz b/data/test/get-registry-downloads.json.gz similarity index 89% rename from data/get-registry-downloads.db.gz rename to data/test/get-registry-downloads.json.gz index 79a8909..e9853cb 100644 Binary files a/data/get-registry-downloads.db.gz and b/data/test/get-registry-downloads.json.gz differ diff --git a/data/test/get-registry-metadata.json.gz b/data/test/get-registry-metadata.json.gz new file mode 100644 index 0000000..501d5bf Binary files /dev/null and b/data/test/get-registry-metadata.json.gz differ diff --git a/data/test/get-registry-signing-keys.json.gz b/data/test/get-registry-signing-keys.json.gz new file mode 100644 index 0000000..9bdb480 Binary files /dev/null and b/data/test/get-registry-signing-keys.json.gz differ diff --git a/data/test/search-packages.json.gz b/data/test/search-packages.json.gz new file mode 100644 index 0000000..e406d94 Binary files /dev/null and b/data/test/search-packages.json.gz differ diff --git a/package.json b/package.json index 19f79bc..36a78b6 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "build": "tsc --noEmit && tsup", "attw": "attw --pack . --ignore-rules cjs-resolves-to-esm", "test": "vitest", - "test:update-db": "bun run scripts/update-test-db.ts", + "test:update-data": "bun run scripts/update-test-data.ts", "test:ci": "vitest run --coverage --bail 1", "lint": "prettier --check .", "format": "prettier --write .", diff --git a/scripts/update-test-data.ts b/scripts/update-test-data.ts new file mode 100644 index 0000000..41959f2 --- /dev/null +++ b/scripts/update-test-data.ts @@ -0,0 +1,3 @@ +import { $ } from "bun"; + +await $`UPDATE_TEST_DATA=true vitest run --bail 1 --test-timeout=60000`; diff --git a/scripts/update-test-db.ts b/scripts/update-test-db.ts deleted file mode 100644 index a232f22..0000000 --- a/scripts/update-test-db.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { $ } from "bun"; - -await $`UPDATE_TEST_DB=true vitest run --bail 1 --test-timeout=60000`; diff --git a/src/get-abbreviated-packument.test.ts b/src/get-abbreviated-packument.test.ts index 1c1976f..9d68313 100644 --- a/src/get-abbreviated-packument.test.ts +++ b/src/get-abbreviated-packument.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getAbbreviatedPackument } from "./get-abbreviated-packument"; -const { loadIntoCache, updateFromCache } = testDb("get-abbreviated-packument"); +const { loadIntoCache, updateFromCache } = testData("get-abbreviated-packument"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-bulk-daily-package-downloads.test.ts b/src/get-bulk-daily-package-downloads.test.ts index afaffd9..64f371a 100644 --- a/src/get-bulk-daily-package-downloads.test.ts +++ b/src/get-bulk-daily-package-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getBulkDailyPackageDownloads } from "./get-bulk-daily-package-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-bulk-daily-package-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-bulk-daily-package-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-bulk-package-downloads.test.ts b/src/get-bulk-package-downloads.test.ts index 7cd54d5..b3d919b 100644 --- a/src/get-bulk-package-downloads.test.ts +++ b/src/get-bulk-package-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getBulkPackageDownloads } from "./get-bulk-package-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-bulk-package-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-bulk-package-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-daily-package-downloads.test.ts b/src/get-daily-package-downloads.test.ts index 570f264..4b66341 100644 --- a/src/get-daily-package-downloads.test.ts +++ b/src/get-daily-package-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getDailyPackageDownloads } from "./get-daily-package-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-daily-package-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-daily-package-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-daily-registry-downloads.test.ts b/src/get-daily-registry-downloads.test.ts index 58a76c1..d4c9c65 100644 --- a/src/get-daily-registry-downloads.test.ts +++ b/src/get-daily-registry-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getDailyRegistryDownloads } from "./get-daily-registry-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-daily-registry-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-daily-registry-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-package-downloads.test.ts b/src/get-package-downloads.test.ts index d35299b..c42b97d 100644 --- a/src/get-package-downloads.test.ts +++ b/src/get-package-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getPackageDownloads } from "./get-package-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-package-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-package-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-package-manifest.test.ts b/src/get-package-manifest.test.ts index cc09636..98a818e 100644 --- a/src/get-package-manifest.test.ts +++ b/src/get-package-manifest.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getPackageManifest } from "./get-package-manifest"; -const { loadIntoCache, updateFromCache } = testDb("get-package-manifest"); +const { loadIntoCache, updateFromCache } = testData("get-package-manifest"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-package-versions-downloads.test.ts b/src/get-package-versions-downloads.test.ts index 080f5a6..81c53a3 100644 --- a/src/get-package-versions-downloads.test.ts +++ b/src/get-package-versions-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getPackageVersionsDownloads } from "./get-package-versions-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-package-versions-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-package-versions-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-packument.test.ts b/src/get-packument.test.ts index 532bcb7..1d37833 100644 --- a/src/get-packument.test.ts +++ b/src/get-packument.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getPackument } from "./get-packument"; -const { loadIntoCache, updateFromCache } = testDb("get-packument"); +const { loadIntoCache, updateFromCache } = testData("get-packument"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-registry-downloads.test.ts b/src/get-registry-downloads.test.ts index 2957220..1fc41db 100644 --- a/src/get-registry-downloads.test.ts +++ b/src/get-registry-downloads.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getRegistryDownloads } from "./get-registry-downloads"; -const { loadIntoCache, updateFromCache } = testDb("get-registry-downloads"); +const { loadIntoCache, updateFromCache } = testData("get-registry-downloads"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-registry-metadata.test.ts b/src/get-registry-metadata.test.ts index d7bba03..5682ce0 100644 --- a/src/get-registry-metadata.test.ts +++ b/src/get-registry-metadata.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getRegistryMetadata } from "./get-registry-metadata"; -const { loadIntoCache, updateFromCache } = testDb("get-registry-metadata"); +const { loadIntoCache, updateFromCache } = testData("get-registry-metadata"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/get-registry-signing-keys.test.ts b/src/get-registry-signing-keys.test.ts index 39fd829..19096a3 100644 --- a/src/get-registry-signing-keys.test.ts +++ b/src/get-registry-signing-keys.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { getRegistrySigningKeys } from "./get-registry-signing-keys"; -const { loadIntoCache, updateFromCache } = testDb("get-registry-signing-keys"); +const { loadIntoCache, updateFromCache } = testData("get-registry-signing-keys"); beforeAll(async () => { await loadIntoCache(); diff --git a/src/search-packages.test.ts b/src/search-packages.test.ts index 213e7cb..a51fe29 100644 --- a/src/search-packages.test.ts +++ b/src/search-packages.test.ts @@ -1,8 +1,8 @@ import { afterAll, beforeAll, expect, test } from "vitest"; -import { testDb } from "../utils/test-db"; +import { testData } from "../utils/test-data"; import { searchPackages } from "./search-packages"; -const { loadIntoCache, updateFromCache } = testDb("search-packages"); +const { loadIntoCache, updateFromCache } = testData("search-packages"); beforeAll(async () => { await loadIntoCache(); diff --git a/utils/test-data.ts b/utils/test-data.ts new file mode 100644 index 0000000..062062b --- /dev/null +++ b/utils/test-data.ts @@ -0,0 +1,36 @@ +import { compressSync, decompressSync, strFromU8, strToU8 } from "fflate"; +import fs from "node:fs/promises"; +import { cache } from "../src/cache"; +import { JsonStrip } from "./json-strip"; + +export const testData = (name: string) => { + const filename = `./data/test/${name}.json.gz`; + return { + loadIntoCache: async () => { + cache.clear(); + if (process.env.UPDATE_TEST_DATA === "true") { + // Don't load old data into cache if we're updating test data with fresh data. + return; + } + const buf = await fs.readFile(filename); + const data = JSON.parse(strFromU8(decompressSync(buf))) as [string, unknown][]; + if (data.length > 0) { + cache.resize(data.length); + for (const [key, value] of data) { + cache.set(key, value); + } + } + }, + updateFromCache: async () => { + if (process.env.UPDATE_TEST_DATA === "true" && cache.size > 0) { + const entries = [...cache.entriesAscending()].map(([key, value]) => [ + key, + JsonStrip.parse(value), + ]); + const data = compressSync(strToU8(JSON.stringify(entries))); + await fs.writeFile(filename, data); + } + cache.clear(); + }, + }; +}; diff --git a/utils/test-db.ts b/utils/test-db.ts deleted file mode 100644 index c25d167..0000000 --- a/utils/test-db.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { compressSync, decompressSync, strFromU8, strToU8 } from "fflate"; -import fs from "node:fs/promises"; -import { cache } from "../src/cache"; -import { JsonStrip } from "../utils/json-strip"; - -export const testDb = (name: string) => { - return { - loadIntoCache: async () => { - cache.clear(); - if (process.env.UPDATE_TEST_DB !== "true") { - const buf = await fs.readFile(`./data/${name}.db.gz`); - const data = JSON.parse(strFromU8(decompressSync(buf))) as [string, unknown][]; - if (data.length > 0) { - cache.resize(data.length); - for (const [key, value] of data) { - cache.set(key, value); - } - } - } - }, - updateFromCache: async () => { - if (process.env.UPDATE_TEST_DB === "true" && cache.size > 0) { - const entries = [...cache.entriesAscending()].map(([key, value]) => [ - key, - JsonStrip.parse(value), - ]); - const data = compressSync(strToU8(JSON.stringify(entries))); - await fs.writeFile(`./data/${name}.db.gz`, data); - } - cache.clear(); - }, - }; -};