-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
934 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: "Vitest" | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- 'releases/*' | ||
pull_request: | ||
branches: | ||
- main | ||
- 'releases/*' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test-idb-cache: | ||
name: Vitest | ||
runs-on: ubuntu-22.04 | ||
|
||
steps: | ||
# 1. Checkout the repository | ||
- uses: actions/checkout@v3 | ||
|
||
# 2. Setup Node.js | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '20' | ||
|
||
# 3. Setup pnpm | ||
- name: Setup pnpm | ||
id: setup-pnpm | ||
uses: pnpm/action-setup@v2 | ||
with: | ||
version: 7.x | ||
|
||
# 4. Cache pnpm dependencies (pnpm store and node_modules) | ||
- name: Cache pnpm dependencies | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.pnpm-store | ||
packages/idb-cache/node_modules | ||
key: ${{ runner.os }}-pnpm-store-idb-cache-${{ hashFiles('**/pnpm-lock.yaml') }}-pnpm-${{ steps.setup-pnpm.outputs.pnpm-version }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store-idb-cache- | ||
# 5. Install dependencies | ||
- name: Install dependencies | ||
run: pnpm install | ||
|
||
# 6. Run Tests and Save Logs | ||
- name: Run Tests for idb-cache | ||
run: | | ||
mkdir -p logs | ||
pnpm --filter packages/idb-cache test > logs/test-output.log 2>&1 || true | ||
# 7. Upload Test Logs (on failure) | ||
- name: Upload Test Logs | ||
if: failure() | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: idb-cache-test-logs | ||
path: logs/test-output.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Least recently used (LRU) cache | ||
export class LRUCache<K, V> { | ||
private maxSize: number; | ||
private cache: Map<K, V>; | ||
|
||
constructor(maxSize = 10000) { | ||
this.maxSize = maxSize; | ||
this.cache = new Map(); | ||
} | ||
|
||
get(key: K): V | undefined { | ||
if (!this.cache.has(key)) return undefined; | ||
const value = this.cache.get(key); | ||
if (value === undefined) return undefined; | ||
this.cache.delete(key); | ||
this.cache.set(key, value); | ||
return value; | ||
} | ||
|
||
set(key: K, value: V): void { | ||
if (this.cache.has(key)) { | ||
this.cache.delete(key); | ||
} else if (this.cache.size >= this.maxSize) { | ||
// Remove least recently used | ||
const firstKey = this.cache.keys().next().value; | ||
if (firstKey !== undefined) { | ||
this.cache.delete(firstKey); | ||
} | ||
} | ||
this.cache.set(key, value); | ||
} | ||
|
||
has(key: K): boolean { | ||
return this.cache.has(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { expect, test, describe } from "vitest"; | ||
import { deterministicUUID, generateUUIDFromHash } from "./utils"; | ||
|
||
const hash1 = | ||
"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; | ||
const hash2 = | ||
"aa26b0aa4af7a749aa1a1aa3c10aa9923f611910772a473f1119a5a4940a0ab27ac115f1a0a1a5f14f11bc117fa67b143732c304cc5fa9aa1a6f57f50021a1ff"; | ||
|
||
describe("generateUUIDFromHash", () => { | ||
test("generates valid UUID v4 format", () => { | ||
const uuid = generateUUIDFromHash(hash1); | ||
|
||
// Check UUID format (8-4-4-4-12 characters) | ||
expect(uuid).toMatch( | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ | ||
); | ||
}); | ||
|
||
test("generates consistent UUIDs for same input", () => { | ||
const uuid1 = generateUUIDFromHash(hash1); | ||
const uuid2 = generateUUIDFromHash(hash1); | ||
|
||
expect(uuid1).toBe(uuid2); | ||
}); | ||
|
||
test("sets correct version (5) in UUID", () => { | ||
const uuid = generateUUIDFromHash(hash1); | ||
expect(uuid.charAt(14)).toBe("5"); | ||
}); | ||
|
||
test("sets correct variant bits in UUID", () => { | ||
const uuid = generateUUIDFromHash(hash1); | ||
|
||
// The 19th character should be 8, 9, a, or b | ||
expect(uuid.charAt(19)).toMatch(/[89ab]/); | ||
}); | ||
|
||
test("generates different UUIDs for different inputs", () => { | ||
const uuid1 = generateUUIDFromHash(hash1); | ||
const uuid2 = generateUUIDFromHash(hash2); | ||
|
||
expect(uuid1).not.toBe(uuid2); | ||
}); | ||
|
||
test("throws error for invalid hash length", () => { | ||
expect(() => generateUUIDFromHash("123")).toThrowError(); | ||
}); | ||
|
||
test("throws error for non-hex characters", () => { | ||
const invalidHash = | ||
"qe26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; // Contains non-hex chars | ||
|
||
expect(() => { | ||
generateUUIDFromHash(invalidHash); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe("deterministicUUID", () => { | ||
test("generates consistent UUID for the same key", async () => { | ||
const key = "test-key"; | ||
const uuid1 = await deterministicUUID(key); | ||
const uuid2 = await deterministicUUID(key); | ||
expect(uuid1).toBe(uuid2); | ||
}); | ||
|
||
test("generates different UUIDs for different keys", async () => { | ||
const uuid1 = await deterministicUUID("test"); | ||
const uuid2 = await deterministicUUID("test2"); | ||
expect(uuid1).not.toBe(uuid2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.