Skip to content

Commit c79d18d

Browse files
test: add tests for populateCache with R2 cache
1 parent 5aefc21 commit c79d18d

File tree

1 file changed

+133
-2
lines changed

1 file changed

+133
-2
lines changed

packages/cloudflare/src/cli/commands/populate-cache.spec.ts

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import path from "node:path";
33

44
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
55
import mockFs from "mock-fs";
6-
import { afterAll, beforeAll, describe, expect, test } from "vitest";
6+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
77

8-
import { getCacheAssets } from "./populate-cache.js";
8+
import { getCacheAssets, populateCache } from "./populate-cache.js";
99

1010
describe("getCacheAssets", () => {
1111
beforeAll(() => {
@@ -68,3 +68,134 @@ describe("getCacheAssets", () => {
6868
`);
6969
});
7070
});
71+
72+
vi.mock("../utils/run-wrangler.js", () => ({
73+
runWrangler: vi.fn(),
74+
}));
75+
76+
vi.mock("./helpers.js", () => ({
77+
getEnvFromPlatformProxy: vi.fn(async () => ({})),
78+
quoteShellMeta: vi.fn((s) => s),
79+
}));
80+
81+
vi.mock("node:child_process", () => ({
82+
spawnSync: vi.fn(() => ({ status: 0 })),
83+
}));
84+
85+
describe("populateCache", () => {
86+
describe("R2_CACHE_NAME", () => {
87+
test("calls runWrangler when rcloneBatch is false", async () => {
88+
const { runWrangler } = await import("../utils/run-wrangler.js");
89+
90+
const buildOptions: BuildOptions = {
91+
outputDir: "/test/output",
92+
} as BuildOptions;
93+
94+
const openNextConfig = {
95+
default: {
96+
override: {
97+
incrementalCache: "cf-r2-incremental-cache",
98+
},
99+
},
100+
};
101+
102+
const wranglerConfig = {
103+
r2_buckets: [
104+
{
105+
binding: "NEXT_INC_CACHE_R2_BUCKET",
106+
bucket_name: "test-bucket",
107+
},
108+
],
109+
};
110+
111+
const populateCacheOptions = {
112+
target: "local" as const,
113+
shouldUsePreviewId: false,
114+
rcloneBatch: false,
115+
};
116+
117+
vi.mocked(runWrangler).mockClear();
118+
119+
mockFs({
120+
"/test/output": {
121+
cache: {
122+
buildID: {
123+
path: {
124+
to: {
125+
"test.cache": JSON.stringify({ data: "test" }),
126+
},
127+
},
128+
},
129+
},
130+
},
131+
});
132+
133+
// For this test we do not need whole configuration, just the part that is being used.
134+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
135+
await populateCache(buildOptions, openNextConfig as any, wranglerConfig as any, populateCacheOptions);
136+
137+
expect(runWrangler).toHaveBeenCalled();
138+
139+
mockFs.restore();
140+
});
141+
142+
test("calls spawnSync with rclone when rcloneBatch is true", async () => {
143+
const { spawnSync } = await import("node:child_process");
144+
145+
const buildOptions: BuildOptions = {
146+
outputDir: "/test/output",
147+
} as BuildOptions;
148+
149+
const openNextConfig = {
150+
default: {
151+
override: {
152+
incrementalCache: "cf-r2-incremental-cache",
153+
},
154+
},
155+
};
156+
157+
const wranglerConfig = {
158+
r2_buckets: [
159+
{
160+
binding: "NEXT_INC_CACHE_R2_BUCKET",
161+
bucket_name: "test-bucket",
162+
},
163+
],
164+
};
165+
166+
const populateCacheOptions = {
167+
target: "local" as const,
168+
shouldUsePreviewId: false,
169+
rcloneBatch: true,
170+
};
171+
172+
vi.mocked(spawnSync).mockClear();
173+
174+
mockFs({
175+
"/test/output": {
176+
cache: {
177+
buildID: {
178+
path: {
179+
to: {
180+
"test.cache": JSON.stringify({ data: "test" }),
181+
},
182+
},
183+
},
184+
},
185+
},
186+
});
187+
188+
// For this test we do not need whole configuration, just the part that is being used.
189+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
190+
await populateCache(buildOptions, openNextConfig as any, wranglerConfig as any, populateCacheOptions);
191+
192+
expect(spawnSync).toHaveBeenCalledWith(
193+
"rclone",
194+
expect.arrayContaining(["copy", expect.any(String), "r2:test-bucket"]),
195+
expect.any(Object)
196+
);
197+
198+
mockFs.restore();
199+
});
200+
});
201+
});

0 commit comments

Comments
 (0)