From 64fcc7f65fd57fb36bf2a32415b5ea15c3109fd7 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 3 Dec 2026 04:45:03 +0800 Subject: [PATCH] test(reference-source): share file entry fixtures (#16099) --- .../ReferenceSourceImportEntries.test.ts | 97 +++++-------------- 1 file changed, 24 insertions(+), 73 deletions(-) diff --git a/src/application/ReferenceSourceImportEntries.test.ts b/src/application/ReferenceSourceImportEntries.test.ts index d9725548f05..09757b69857 100644 --- a/src/application/ReferenceSourceImportEntries.test.ts +++ b/src/application/ReferenceSourceImportEntries.test.ts @@ -9,6 +9,17 @@ import { } from "../domain/referenceSourceGraph.js"; import type { ReferenceSourceRead } from "../reference/ReferenceSourceReaderTypes.js"; +const sourceFile = ( + path: string, + content: string, +): ReferenceSourceRead["entries"][number] => ({ + status: "read", + kind: "file", + path, + bytes: Buffer.from(content), + size: Buffer.byteLength(content), +}); + const readSourceFiles = ( files: readonly (readonly [ path: string, @@ -17,13 +28,7 @@ const readSourceFiles = ( ])[], ): ReferenceSourceRead => ({ root: "/selected/source", - entries: files.map(([path, content]) => ({ - status: "read", - kind: "file", - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + entries: files.map(([path, content]) => sourceFile(path, content)), bytesRead: files.reduce( (sum, [, content]) => sum + Buffer.byteLength(content), 0, @@ -250,21 +255,7 @@ it("resolves declared package mains and index fallback from source-owned fields" ["json-package/index.js", 'module.exports = "fallback index";\n'], ["emitted.ts", 'export const value = "source counterpart";\n'], ] as const; - const read: ReferenceSourceRead = { - root: "/selected/source", - entries: files.map(([path, content]) => ({ - status: "read", - kind: "file", - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), - bytesRead: files.reduce( - (sum, [, content]) => sum + Buffer.byteLength(content), - 0, - ), - limitations: [], - }; + const read = readSourceFiles(files); const previous = Object.getOwnPropertyDescriptor(Object.prototype, "main"); let parsed: ReturnType; @@ -414,30 +405,20 @@ it("resolves extensionless imports and requires to .mts and .cts files and direc }); it("does not infer missing candidates or manifests from partial directory enumeration", () => { - const file = ( - path: string, - content: string, - ): ReferenceSourceRead["entries"][number] => ({ - status: "read", - kind: "file", - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - }); const read: ReferenceSourceRead = { root: "/selected/source", bytesRead: 0, limitations: [], entries: [ - file("package.json", '{"type":"commonjs"}'), - file( + sourceFile("package.json", '{"type":"commonjs"}'), + sourceFile( "main.cjs", 'require("./pkg"); require("./other"); require("./exact.js");', ), - file("pkg/index.js", "module.exports = 1;"), - file("other/index.js", "module.exports = 1;"), - file("exact.js", "module.exports = 1;"), - file("pkg/source.js", 'require("../exact.js");'), + sourceFile("pkg/index.js", "module.exports = 1;"), + sourceFile("other/index.js", "module.exports = 1;"), + sourceFile("exact.js", "module.exports = 1;"), + sourceFile("pkg/source.js", 'require("../exact.js");'), { status: "failed", kind: "directory", @@ -492,13 +473,7 @@ it.each(["failed", "excluded"] as const)( bytesRead: 0, limitations: [], entries: [ - ...files.map(([path, content]) => ({ - status: "read" as const, - kind: "file" as const, - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + ...files.map(([path, content]) => sourceFile(path, content)), ...(state === "failed" ? [ { @@ -548,13 +523,7 @@ it("retains observed manifests and closer completed scopes inside a partial inve bytesRead: 0, limitations: [], entries: [ - ...files.map(([path, content]) => ({ - status: "read" as const, - kind: "file" as const, - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + ...files.map(([path, content]) => sourceFile(path, content)), { status: "failed", kind: "directory", @@ -601,13 +570,7 @@ it("keeps ambiguous directory coverage unknown while honoring exact directory id bytesRead: 0, limitations: [], entries: [ - ...files.map(([path, content]) => ({ - status: "read" as const, - kind: "file" as const, - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + ...files.map(([path, content]) => sourceFile(path, content)), { status: "failed", kind: "directory", @@ -644,13 +607,7 @@ it("does not skip an ambiguous higher priority file for an observed extension fa root: "/selected/source", bytesRead: 0, limitations: [], - entries: files.map(([path, content]) => ({ - status: "read" as const, - kind: "file" as const, - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + entries: files.map(([path, content]) => sourceFile(path, content)), }, [], ); @@ -673,13 +630,7 @@ it("prefers a later exact filename over an earlier unique canonical candidate", root: "/selected/source", bytesRead: 0, limitations: [], - entries: files.map(([path, content]) => ({ - status: "read" as const, - kind: "file" as const, - path, - bytes: Buffer.from(content), - size: Buffer.byteLength(content), - })), + entries: files.map(([path, content]) => sourceFile(path, content)), }, [], );