perf(studio): cache nitro scan, stream progress, virtualize cleanup list
This commit is contained in:
1 parent
c916e42572
commit
ba81d16f00
5 files changed
+727
-257
No files matched your search
@@ -1,4 +1,5 @@
|
||||
// @vitest-environment node
|
||||
import { createHash } from "node:crypto";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type AnyFn = (...args: any[]) => any;
|
||||
@@ -130,6 +131,13 @@ function mockReaddir(byDir: Record<string, string[]>): void {
|
||||
readdirFn.mockImplementation(async (dir: string) => byDir[dir] ?? []);
|
||||
}
|
||||
|
||||
/** Replicates `dirNameSignature` in the service so cache fixtures match. */
|
||||
function dirSignature(names: string[]): string {
|
||||
const hash = createHash("sha1");
|
||||
for (const name of names) hash.update(name).update("\n");
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
describe("scanFakeBrokenNitros", () => {
|
||||
it("classifies fake (no matching item) and broken (unparseable) bundles", async () => {
|
||||
mockReaddir({
|
||||
@@ -233,6 +241,73 @@ describe("scanFakeBrokenNitros", () => {
|
||||
expect(result.fake[0].size).toBe(900);
|
||||
expect(result.fake[0].lastModified).toBe(2000);
|
||||
});
|
||||
|
||||
it("serves results from the on-disk cache when directories are unchanged", async () => {
|
||||
mockReaddir({
|
||||
"/assets/nitro": ["chair.nitro", "ghost.nitro"],
|
||||
});
|
||||
const dirs = {
|
||||
"nitro\u0000/assets/nitro": dirSignature(["chair.nitro", "ghost.nitro"]),
|
||||
"swf\u0000/assets/swf": dirSignature([]),
|
||||
"icon\u0000/assets/icons": dirSignature([]),
|
||||
};
|
||||
const cachedResult = {
|
||||
fake: [
|
||||
{
|
||||
fileName: "ghost.nitro",
|
||||
base: "ghost",
|
||||
size: 500,
|
||||
dirs: ["/assets/nitro"],
|
||||
lastModified: 0,
|
||||
error: undefined,
|
||||
},
|
||||
],
|
||||
broken: [],
|
||||
orphanedSwf: [],
|
||||
orphanedIcon: [],
|
||||
total: 4,
|
||||
};
|
||||
existsFn.mockImplementation((filePath: string) =>
|
||||
filePath.includes("scan-cache.json"),
|
||||
);
|
||||
readFileFn.mockImplementation(async (filePath: string) => {
|
||||
if (filePath.includes("scan-cache.json")) {
|
||||
return JSON.stringify({ dirs, result: cachedResult });
|
||||
}
|
||||
return Buffer.from("nitro-data");
|
||||
});
|
||||
|
||||
const result = await scanFakeBrokenNitros();
|
||||
|
||||
// `total: 4` could never come from a fresh scan of 2 files — the cache
|
||||
// result came back verbatim, so classification was skipped entirely.
|
||||
expect(result).toEqual(expect.objectContaining(cachedResult));
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.total).toBe(4);
|
||||
expect(executeFn).not.toHaveBeenCalled();
|
||||
expect(statFn).not.toHaveBeenCalled();
|
||||
expect(readFileFn).toHaveBeenCalledTimes(1);
|
||||
expect(writeFileFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("force bypasses the cache and re-validates the directories", async () => {
|
||||
mockReaddir({
|
||||
"/assets/nitro": ["chair.nitro", "ghost.nitro"],
|
||||
});
|
||||
// A matching-on-disk cache exists, but `force: true` ignores it.
|
||||
existsFn.mockImplementation(() => true);
|
||||
readFileFn.mockResolvedValue(Buffer.from("nitro-data"));
|
||||
parseNitroBundle.mockReturnValue({});
|
||||
statFn.mockImplementation(async () => ({ size: 512, mtimeMs: 0 }));
|
||||
|
||||
const result = await scanFakeBrokenNitros({ force: true });
|
||||
|
||||
expect(result.cached).toBeUndefined();
|
||||
expect(result.fake.map((e) => e.fileName)).toEqual(["ghost.nitro"]);
|
||||
expect(result.broken).toEqual([]);
|
||||
expect(result.total).toBe(2);
|
||||
expect(statFn).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteNitroCleanupFiles", () => {
|
||||
|
||||
Reference in new issue
Block a user