|
| 1 | +import { describe, test, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { createCacheKey } from "@opennextjs/aws/utils/cache.js"; |
| 3 | + |
| 4 | +describe("createCacheKey", () => { |
| 5 | + const originalEnv = process.env; |
| 6 | + const originalGlobalThis = globalThis as any; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + vi.resetModules(); |
| 10 | + process.env = { ...originalEnv }; |
| 11 | + |
| 12 | + // Mock globalThis.openNextConfig |
| 13 | + if (!globalThis.openNextConfig) { |
| 14 | + globalThis.openNextConfig = { |
| 15 | + dangerous: {}, |
| 16 | + }; |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + process.env = originalEnv; |
| 22 | + globalThis.openNextConfig = originalGlobalThis.openNextConfig; |
| 23 | + }); |
| 24 | + |
| 25 | + test("prepends build ID for non-data cache entries", () => { |
| 26 | + process.env.NEXT_BUILD_ID = "test-build-id"; |
| 27 | + const key = "test-key"; |
| 28 | + |
| 29 | + const result = createCacheKey(key, false); |
| 30 | + |
| 31 | + expect(result).toBe("test-build-id/test-key"); |
| 32 | + }); |
| 33 | + |
| 34 | + test("prepends build ID for data cache when persistentDataCache is not enabled", () => { |
| 35 | + process.env.NEXT_BUILD_ID = "test-build-id"; |
| 36 | + globalThis.openNextConfig.dangerous.persistentDataCache = false; |
| 37 | + const key = "test-key"; |
| 38 | + |
| 39 | + const result = createCacheKey(key, true); |
| 40 | + |
| 41 | + expect(result).toBe("test-build-id/test-key"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("does not prepend build ID for data cache when persistentDataCache is enabled", () => { |
| 45 | + process.env.NEXT_BUILD_ID = "test-build-id"; |
| 46 | + globalThis.openNextConfig.dangerous.persistentDataCache = true; |
| 47 | + const key = "test-key"; |
| 48 | + |
| 49 | + const result = createCacheKey(key, true); |
| 50 | + |
| 51 | + expect(result).toBe("test-key"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("handles missing build ID", () => { |
| 55 | + process.env.NEXT_BUILD_ID = undefined; |
| 56 | + const key = "test-key"; |
| 57 | + |
| 58 | + const result = createCacheKey(key, false); |
| 59 | + |
| 60 | + expect(result).toBe("test-key"); |
| 61 | + }); |
| 62 | +}); |
0 commit comments