|
1 | | -import { existsSync } from "node:fs"; |
2 | | -import { readdir, readFile } from "node:fs/promises"; |
3 | | -import path from "node:path"; |
4 | | -import { brotliDecompressSync, gunzipSync } from "node:zlib"; |
5 | 1 | import { describe, expect, it } from "vitest"; |
| 2 | +import { getBuildOutputDirs, getFiles, readFileContent } from "~/utils/build-output-utils"; |
6 | 3 |
|
7 | 4 | // Avoid full pattern to exclude this file from scan |
8 | 5 | const SECRET_MARKER = new RegExp(`${"MyServer"}${"SuperSecretUniqueString"}\\d+`, "g"); |
9 | 6 | const ALL_FILE_EXTENSIONS = /\.(ts|tsx|js|jsx|mjs|cjs|mts|cts|css|map|gz|br)$/; |
10 | 7 |
|
11 | 8 | describe("server code does not leak to client bundle", () => { |
12 | 9 | it("verifies secret markers are server-only and not in client output", async () => { |
13 | | - const appRoot = process.cwd(); |
14 | | - const sourceRoot = path.join(appRoot, "src"); |
15 | | - const serverOutputRoot = path.join(appRoot, ".output/server"); |
16 | | - const clientOutputRoot = path.join(appRoot, ".output/public"); |
17 | | - |
18 | | - // Verify required directories exist |
19 | | - expect(existsSync(sourceRoot), `Source dir not found: ${sourceRoot}`).toBe(true); |
20 | | - expect( |
21 | | - existsSync(serverOutputRoot), |
22 | | - `Server output dir not found: ${serverOutputRoot}. Did you run the build? (pnpm --filter tests run build)`, |
23 | | - ).toBe(true); |
24 | | - expect( |
25 | | - existsSync(clientOutputRoot), |
26 | | - `Client output dir not found: ${clientOutputRoot}. Did you run the build? (pnpm --filter tests run build)`, |
27 | | - ).toBe(true); |
| 10 | + const { sourceRoot, serverOutputRoot, clientOutputRoot } = getBuildOutputDirs(); |
28 | 11 |
|
29 | 12 | // Collect and validate markers from source code |
30 | 13 | const sourceMarkerCounts = await countSourceMarkers(sourceRoot); |
@@ -77,22 +60,3 @@ async function countSourceMarkers(rootDir: string) { |
77 | 60 | } |
78 | 61 | return markerCounts; |
79 | 62 | } |
80 | | - |
81 | | -async function getFiles(dir: string, fileRegex: RegExp): Promise<string[]> { |
82 | | - const entries = await readdir(dir, { recursive: true, withFileTypes: true }); |
83 | | - return entries |
84 | | - .filter(e => e.isFile() && fileRegex.test(e.name)) |
85 | | - .map(e => path.join(e.parentPath, e.name)); |
86 | | -} |
87 | | - |
88 | | -async function readFileContent(filePath: string) { |
89 | | - if (filePath.endsWith(".br")) { |
90 | | - return brotliDecompressSync(await readFile(filePath)).toString("utf-8"); |
91 | | - } |
92 | | - |
93 | | - if (filePath.endsWith(".gz")) { |
94 | | - return gunzipSync(await readFile(filePath)).toString("utf-8"); |
95 | | - } |
96 | | - |
97 | | - return readFile(filePath, "utf-8"); |
98 | | -} |
0 commit comments