Skip to content

Commit

Permalink
test(appCache): add missing tests (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly authored Jan 28, 2025
1 parent 12c51df commit 8ffc961
Showing 1 changed file with 307 additions and 0 deletions.
307 changes: 307 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
// Import Node.js Dependencies
import assert from "node:assert/strict";
import { before, describe, it } from "node:test";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";

// Import Third-party Dependencies
import cacache from "cacache";

// Import Internal Dependencies
import { appCache } from "../src/http-server/cache.js";

// CONSTANTS
const kPayloadsPath = path.join(os.homedir(), ".nsecure", "payloads");

describe("appCache", () => {
before(() => {
appCache.prefix = "test_runner";
});

it("should update and get config", async() => {
await appCache.updateConfig({ foo: "bar" });

const updated = await appCache.getConfig();
assert.deepEqual(updated, { foo: "bar" });
});

it("should write payload into ~/.nsecure/payloads", (t) => {
let writePath;
let writeValue;
t.mock.method(fs, "writeFileSync", (path, value) => {
writePath = path;
writeValue = value;
});

appCache.updatePayload("foo/bar", { foo: "bar" });

assert.equal(writePath, path.join(kPayloadsPath, "foo-bar"));
assert.equal(writeValue, JSON.stringify({ foo: "bar" }));
});

it("getPayload should return the payload", async(t) => {
t.mock.method(fs, "readFileSync", () => JSON.stringify({ foo: "bar" }));

const payload = await appCache.getPayload("foo/bar");

assert.deepEqual(payload, { foo: "bar" });
});

it("getPayload should throw", async(t) => {
t.mock.method(fs, "readFileSync", () => {
throw new Error("boo");
});

await assert.rejects(async() => appCache.getPayload("foo/bar"), {
message: "boo"
});
});

it("getPayloadOrNull should return payload", async(t) => {
t.mock.method(fs, "readFileSync", () => JSON.stringify({ foo: "bar" }));

const payload = await appCache.getPayloadOrNull("foo/bar");

assert.deepEqual(payload, { foo: "bar" });
});

it("getPayloadOrNull should return null", async(t) => {
t.mock.method(fs, "readFileSync", () => {
throw new Error("boo");
});

const payload = await appCache.getPayloadOrNull("foo/bar");

assert.equal(payload, null);
});

it("should update and get payloadsList", async() => {
await appCache.updatePayloadsList({ foo: "bar" });

const updated = await appCache.payloadsList();
assert.deepEqual(updated, { foo: "bar" });
});

it("payloadList should throw", async(t) => {
t.mock.method(cacache, "get", () => {
throw new Error("boo");
});

await assert.rejects(async() => appCache.payloadsList(), {
message: "boo"
});
});

it("should init payloadsList when starting from zero", async(t) => {
appCache.startFromZero = true;
t.mock.method(fs, "readdirSync", () => []);
t.mock.method(cacache, "get", () => {
throw new Error("boo");
});

await appCache.initPayloadsList();

t.mock.reset();

const payloadsList = await appCache.payloadsList();

assert.deepEqual(payloadsList, {
lru: [],
current: null,
older: [],
lastUsed: {},
root: null
});
});

it("should init payloadsList with the root payload json", async(t) => {
appCache.startFromZero = false;
t.mock.method(fs, "readdirSync", () => []);
t.mock.method(fs, "readFileSync", () => JSON.stringify({
rootDependencyName: "test_runner",
dependencies: {
test_runner: {
versions: {
"1.0.0": {}
}
}
}
}));
t.mock.method(fs, "writeFileSync", () => void 0);
t.mock.method(cacache, "get", () => {
throw new Error("boo");
});
t.mock.method(Date, "now", () => 1234567890);

await appCache.initPayloadsList();

t.mock.reset();

const payloadsList = await appCache.payloadsList();

assert.deepEqual(payloadsList, {
lru: ["[email protected]"],
current: "[email protected]",
older: [],
lastUsed: { "[email protected]": 1234567890 },
root: "[email protected]"
});
});

it("should init payloadsList.older with already scanned payloads", async(t) => {
t.mock.method(fs, "readdirSync", () => ["[email protected]", "[email protected]"]);
t.mock.method(cacache, "get", () => {
throw new Error("boo");
});

await appCache.initPayloadsList();

t.mock.reset();

const payloadsList = await appCache.payloadsList();

assert.deepEqual(payloadsList, {
older: ["[email protected]", "[email protected]"],
current: null,
lru: []
});
});

it("should remove payload from disk", (t) => {
let removedPath;
t.mock.method(fs, "rmSync", (path) => {
removedPath = path;
});

appCache.removePayload("foo/bar");

assert.equal(removedPath, path.join(kPayloadsPath, "foo-bar"));
});

it("should not remove the last LRU when LRU is not full", async(t) => {
t.mock.method(cacache, "get", () => {
return {
data: {
toString: () => JSON.stringify({
lru: ["foo"],
older: ["bar"],
lastUsed: { foo: 1234567890 },
foo: "bar"
})
}
};
});

const result = await appCache.removeLastLRU();

assert.deepEqual(result, {
lru: ["foo"],
older: ["bar"],
lastUsed: { foo: 1234567890 },
foo: "bar"
});
});

it("should remove the last LRU when LRU is full", async(t) => {
t.mock.method(cacache, "get", () => {
return {
data: {
toString: () => JSON.stringify({
lru: ["foo", "foz", "bar"],
older: ["boz"],
lastUsed: {
foo: 123,
foz: 1234,
bar: 12345
},
foo: "bar"
})
}
};
});

const result = await appCache.removeLastLRU();

assert.deepEqual(result, {
lru: ["foz", "bar"],
older: ["boz", "foo"],
lastUsed: {
foo: 123,
foz: 1234,
bar: 12345
},
foo: "bar"
});
});

it("should set local root payload", async(t) => {
t.mock.method(fs, "writeFileSync", () => void 0);
t.mock.method(Date, "now", () => 1234567890);
await appCache.updatePayloadsList({
lru: [],
current: null,
older: [],
lastUsed: {},
root: null
});
const payload = {
rootDependencyName: "test_runner-local",
dependencies: {
"test_runner-local": {
versions: {
"1.0.0": {}
}
}
}
};
await appCache.setRootPayload(payload, { local: true });

const result = await appCache.payloadsList();

assert.deepEqual(result, {
lru: ["[email protected]#local"],
current: "[email protected]#local",
older: [],
lastUsed: {
"[email protected]#local": 1234567890
},
root: "[email protected]#local"
});
});

it("should set normal root payload", async(t) => {
t.mock.method(fs, "writeFileSync", () => void 0);
t.mock.method(Date, "now", () => 1234567890);
await appCache.updatePayloadsList({
lru: [],
current: null,
older: [],
lastUsed: {},
root: null
});
const payload = {
rootDependencyName: "test_runner-local",
dependencies: {
"test_runner-local": {
versions: {
"1.0.0": {}
}
}
}
};
await appCache.setRootPayload(payload, {});

const result = await appCache.payloadsList();

assert.deepEqual(result, {
lru: ["[email protected]"],
current: "[email protected]",
older: [],
lastUsed: {
"[email protected]": 1234567890
},
root: "[email protected]"
});
});
});

0 comments on commit 8ffc961

Please sign in to comment.