Skip to content

Commit df59d01

Browse files
committed
Added vitests for blob options processor
1 parent b66b208 commit df59d01

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

vitest/tests/archive.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ describe("removeAllDecompressedFiles", () => {
158158

159159
describe("decompressArchive", () => {
160160
afterEach(vi.clearAllMocks);
161+
161162
const option: t.RemoteBlobOption = { url: "", dest: "" };
162163
const details: t.DestDetails = {
163164
fileName: "",

vitest/tests/processor.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024, Mattias Aabmets
5+
*
6+
* The contents of this file are subject to the terms and conditions defined in the License.
7+
* You may not use, modify, or distribute this file except in compliance with the License.
8+
*
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/
11+
12+
import { processBlobOption } from "@src/processor";
13+
import * as u from "@testutils";
14+
import { afterEach, describe, expect, it, vi } from "vitest";
15+
16+
describe("processBlobOption", () => {
17+
afterEach(vi.restoreAllMocks);
18+
19+
it("should download when historical entry does not exist", async () => {
20+
const [option] = u.setupProcessorTest();
21+
const procRet = await processBlobOption({ contents: {}, option });
22+
expect(procRet.skipDownload).toEqual(false);
23+
});
24+
25+
it("should download when historical entry exists and alwaysPull is true", async () => {
26+
const [option, contents] = u.setupProcessorTest({ alwaysPull: true });
27+
const procRet = await processBlobOption({ contents, option });
28+
expect(procRet.skipDownload).toEqual(false);
29+
});
30+
31+
it("should skip download when all decompressed files exist", async () => {
32+
const [option, contents] = u.setupProcessorTest({
33+
decompress: true,
34+
decompFilesExist: true,
35+
filesList: ["asdfg.txt"],
36+
});
37+
const procRet = await processBlobOption({ contents, option });
38+
expect(procRet.skipDownload).toEqual(true);
39+
});
40+
41+
it("should download when all decompressed files do not exist", async () => {
42+
const [option, contents] = u.setupProcessorTest({
43+
decompress: true,
44+
decompFilesExist: false,
45+
filesList: ["asdfg.txt"],
46+
});
47+
const procRet = await processBlobOption({ contents, option });
48+
expect(procRet.skipDownload).toEqual(false);
49+
});
50+
51+
it("should skip download when option file file exists", async () => {
52+
const [option, contents] = u.setupProcessorTest({ newFileExists: true });
53+
const procRet = await processBlobOption({ contents, option });
54+
expect(procRet.skipDownload).toEqual(true);
55+
});
56+
57+
it("should download when historical file exists and option file does not", async () => {
58+
const [option, contents, unlinkMock] = u.setupProcessorTest({ oldFileExists: true });
59+
const procRet = await processBlobOption({ contents, option });
60+
expect(procRet.skipDownload).toEqual(false);
61+
expect(unlinkMock).toHaveBeenCalledOnce();
62+
});
63+
});

vitest/testutils/index.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
*/
1111

1212
import crypto from "node:crypto";
13+
import fsp from "node:fs/promises";
1314
import { tmpdir } from "node:os";
1415
import path from "node:path";
16+
import archive from "@src/archive";
17+
import utils from "@src/utils";
1518
import type * as t from "@types";
19+
import { type MockInstance, vi } from "vitest";
1620

1721
export function getTempDirPath() {
18-
const randString = crypto.randomBytes(8).toString("hex");
22+
const randString = crypto.randomBytes(16).toString("hex");
1923
return path.join(tmpdir(), "vitest", randString);
2024
}
2125

@@ -51,3 +55,54 @@ export function getMustDownload(prettyName1?: string, prettyName2?: string): t.P
5155
} as t.ProcessorReturn,
5256
];
5357
}
58+
59+
export interface SetupArgs {
60+
newFileExists?: boolean;
61+
oldFileExists?: boolean;
62+
alwaysPull?: boolean;
63+
decompress?: boolean | t.DecompressionOptions;
64+
decompFilesExist?: boolean;
65+
filesList?: string[];
66+
}
67+
68+
export type SetupReturn = [t.RemoteBlobOption, t.HistoryFileContents, MockInstance];
69+
70+
export function setupProcessorTest(args?: SetupArgs): SetupReturn {
71+
const option: t.RemoteBlobOption = {
72+
url: "https://example.com/file.zip",
73+
dest: "some/path/testFile.zip",
74+
alwaysPull: args?.alwaysPull ?? false,
75+
decompress: args?.decompress ?? false,
76+
};
77+
const blobDigest = utils.digestRemoteBlobOption(option);
78+
const decompDigest = archive.digestDecompressionOptions(option.decompress);
79+
const contents: t.HistoryFileContents = {
80+
[blobDigest]: {
81+
url: "https://example.com/file.zip",
82+
dest: "/some/path/testFile.zip",
83+
blobOptionsDigest: blobDigest,
84+
decompression: {
85+
optionsDigest: decompDigest,
86+
filesList: args?.filesList ?? [],
87+
},
88+
},
89+
};
90+
const getDestDetails = (exists?: boolean) => {
91+
return {
92+
filePath: "/some/path/testFile.zip",
93+
fileExists: !!exists,
94+
dirPath: "/some/path",
95+
dirExists: !!exists,
96+
fileName: "testFile.zip",
97+
};
98+
};
99+
vi.spyOn(utils, "getDestDetails")
100+
.mockImplementationOnce(() => getDestDetails(args?.newFileExists))
101+
.mockImplementationOnce(() => getDestDetails(args?.oldFileExists));
102+
if (args?.decompress) {
103+
vi.spyOn(archive, "allDecompressedFilesExist").mockResolvedValue(!!args?.decompFilesExist);
104+
vi.spyOn(archive, "removeAllDecompressedFiles").mockImplementation(() => Promise.resolve());
105+
}
106+
const unlinkMock = vi.spyOn(fsp, "unlink").mockResolvedValue();
107+
return [option, contents, unlinkMock];
108+
}

0 commit comments

Comments
 (0)