Skip to content

Commit

Permalink
wip: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 16, 2024
1 parent c357ada commit f3fe097
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ <h2>Message log</h2>
*/
async function getItemWithExpiry(key) {
const db = await openDB();
const transaction = db.transaction(STORE_NAME, "readonly");
const transaction = db.transaction(STORE_NAME, "readwrite");
const store = transaction.objectStore(STORE_NAME);

return new Promise((resolve, reject) => {
Expand Down
53 changes: 33 additions & 20 deletions auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { JSDOM } from "jsdom";
import fs from "fs";
import path from "path";
import * as crypto from "crypto";
import { indexedDB } from "fake-indexeddb";

const html = fs.readFileSync(path.resolve(__dirname, "./index.html"), "utf8");

let dom;
let TKHQ;

describe("TKHQ", () => {
beforeEach(() => {
beforeEach(async () => {
dom = new JSDOM(html, {
// Necessary to run script tags
runScripts: "dangerously",
Expand All @@ -24,50 +25,62 @@ describe("TKHQ", () => {
value: crypto.webcrypto,
});

Object.defineProperty(dom.window, "indexedDB", {
value: indexedDB,
});

TKHQ = dom.window.TKHQ;
await TKHQ.openDB();
});

it("gets and sets items with expiry localStorage", async () => {
// Set a TTL of 1000ms
TKHQ.setItemWithExpiry("k", "v", 1000);
let item = JSON.parse(dom.window.localStorage.getItem("k"));
expect(item.value).toBe("v");
expect(item.expiry).toBeTruthy();
await TKHQ.setItemWithExpiry("k", "v", 1000);

// TODO
// probably need wrappers/handlers to manipulate indexeddb directly
// let item = JSON.parse(dom.window.localStorage.getItem("k"));
// expect(item.value).toBe("v");
// expect(item.expiry).toBeTruthy();

// Get item that has not expired yet
item = TKHQ.getItemWithExpiry("k");
let item = await TKHQ.getItemWithExpiry("k");
expect(item).toBe("v");

// Set a TTL of 500ms
TKHQ.setItemWithExpiry("a", "b", 500);
setTimeout(() => {
const expiredItem = TKHQ.getItemWithExpiry("a");
await TKHQ.setItemWithExpiry("a", "b", 500);
setTimeout(async () => {
const expiredItem = await TKHQ.getItemWithExpiry("a");
expect(expiredItem).toBeNull();
}, 600); // Wait for 600ms to ensure the item has expired

// Returns null if getItemWithExpiry is called for item without expiry
dom.window.localStorage.setItem("k", JSON.stringify({ value: "v" }));
item = TKHQ.getItemWithExpiry("k");
expect(item).toBeNull();
// TODO
// Returns null if getItemWithExpiry is called for an item that does not have an expiry
// dom.window.localStorage.setItem("k", JSON.stringify({ value: "v" }));
// item = TKHQ.getItemWithExpiry("k");
// expect(item).toBeNull();
});

it("gets and sets embedded key in localStorage", async () => {
expect(TKHQ.getEmbeddedKey()).toBe(null);
it("gets, sets, and resets embedded key in localStorage", async () => {
expect(await TKHQ.getEmbeddedKey()).toBe(null);

// Set a dummy "key"
TKHQ.setEmbeddedKey({ foo: "bar" });
expect(TKHQ.getEmbeddedKey()).toEqual({ foo: "bar" });
await TKHQ.setEmbeddedKey({ foo: "bar" });
expect(await TKHQ.getEmbeddedKey()).toEqual({ foo: "bar" });

await TKHQ.onResetEmbeddedKey();
expect(await TKHQ.getEmbeddedKey()).toBe(null);
});

it("inits embedded key and is idempotent", async () => {
expect(TKHQ.getEmbeddedKey()).toBe(null);
expect(await TKHQ.getEmbeddedKey()).toBe(null);
await TKHQ.initEmbeddedKey();
var generatedKey = TKHQ.getEmbeddedKey();
var generatedKey = await TKHQ.getEmbeddedKey();
expect(generatedKey).not.toBeNull();

// This should have no effect; generated key should stay the same
await TKHQ.initEmbeddedKey();
expect(TKHQ.getEmbeddedKey()).toEqual(generatedKey);
expect(await TKHQ.getEmbeddedKey()).toEqual(generatedKey);
});

it("generates P256 keys", async () => {
Expand Down
16 changes: 16 additions & 0 deletions auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"@babel/preset-env": "^7.22.20",
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.1.3",
"jsdom": "^22.1.0",
"babel-jest": "^29.7.0",
"fake-indexeddb": "^6.0.0",
"jest": "^29.7.0",
"serve": "^14.2.1",
"prettier": "^2.8.4"
"jsdom": "^22.1.0",
"prettier": "^2.8.4",
"serve": "^14.2.1"
}
}

0 comments on commit f3fe097

Please sign in to comment.