add some cache functionality tests

This commit is contained in:
Dallin Romney 2025-06-17 02:30:04 -07:00
parent 79f0729113
commit 3678a85dff

View file

@ -1,6 +1,6 @@
import * as fs from "node:fs";
import { beforeEach, expect, test, vi } from "vitest";
import { getCertificateContent } from "./certs.js";
import { CertsCache, getCertificateContent } from "./certs.js";
// Mock fs module
vi.mock("node:fs", () => ({
@ -108,3 +108,80 @@ test("getCertificateContent should handle data URI with special characters in UR
expect(result).toBe(testCert);
});
test("CertsCache.getCachedCustomCert should cache and return certificate content", async () => {
const certsCache = CertsCache.getInstance();
const filePath = "/path/to/custom/cert.pem";
const expectedContent = "custom cert content";
mockReadFileSync.mockReturnValue(expectedContent);
const cert1 = await certsCache.getCachedCustomCert(filePath);
expect(cert1).toBe(expectedContent);
expect(mockReadFileSync).toHaveBeenCalledTimes(1);
expect(mockReadFileSync).toHaveBeenCalledWith(filePath, "utf8");
// Call again to check if it's cached
const cert2 = await certsCache.getCachedCustomCert(filePath);
expect(cert2).toBe(expectedContent);
// readFileSync should not be called again
expect(mockReadFileSync).toHaveBeenCalledTimes(1);
});
test("CertsCache.getAllCachedCustomCerts should return all cached custom certs", async () => {
const certsCache = CertsCache.getInstance();
const filePaths = ["/path/to/cert1.pem", "/path/to/cert2.pem"];
const expectedContent1 = "content of cert1";
const expectedContent2 = "content of cert2";
mockReadFileSync.mockReturnValueOnce(expectedContent1);
mockReadFileSync.mockReturnValueOnce(expectedContent2);
const certs = await certsCache.getAllCachedCustomCerts(filePaths);
expect(certs).toEqual([expectedContent1, expectedContent2]);
expect(mockReadFileSync).toHaveBeenCalledTimes(2);
});
test("CertsCache.getCa should return combined CA when caBundlePath is provided", async () => {
const certsCache = CertsCache.getInstance();
const fixedCa = ["fixed CA cert"];
const customCertPath = "/path/to/custom/cert.pem";
const customCertContent = "custom cert content";
// Directly set _fixedCa to avoid initializing it with real data
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
certsCache._fixedCa = fixedCa;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
certsCache._initialized = true;
mockReadFileSync.mockReturnValue(customCertContent);
const ca = await certsCache.getCa(customCertPath);
expect(ca).toEqual([...fixedCa, customCertContent]);
});
test("CertsCache.clear should clear custom certs and reset initialized flag", () => {
const certsCache = CertsCache.getInstance();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
certsCache._customCerts.set("key", "value");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
certsCache._initialized = true;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
certsCache._fixedCa = ["test"];
certsCache.clear();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(certsCache._customCerts.size).toBe(0);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(certsCache._initialized).toBe(false);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(certsCache._fixedCa).toEqual([]);
});