import fs from "node:fs"; import path from "node:path"; export const OFFICIAL_FIXTURE_AREAS = [ "examples", "tests/ModelRefineTests", "tests/Step", "tests", ]; function inside(parent, candidate) { const relative = path.relative(parent, candidate); return relative === "" || (relative && !relative.startsWith("..") && !path.isAbsolute(relative)); } function filesIn(directory) { try { return fs.readdirSync(directory, { withFileTypes: true }).filter(entry => entry.isFile()).map(entry => entry.name); } catch { return []; } } function resolveDirectoryInsensitive(dataRoot, area) { let current = dataRoot; for (const segment of area.split(/[\\/]/).filter(Boolean)) { const exact = path.join(current, segment); if (fs.existsSync(exact)) { current = exact; continue; } let entries = []; try { entries = fs.readdirSync(current, { withFileTypes: true }); } catch { return exact; } const actual = entries.find(entry => entry.isDirectory() && entry.name.toLowerCase() === segment.toLowerCase()); if (!actual) return exact; current = path.join(current, actual.name); } return current; } export function resolveOfficialFixture(root, input, extensions) { if (typeof input !== "string" || !input.trim()) throw new Error("Fixture basename or data-relative path is required"); const raw = input.trim().replaceAll("\\", "/"); const dataRoot = path.resolve(root, "freecad-source/data"); let requested = raw; if (path.isAbsolute(raw)) { const candidate = path.resolve(raw); if (!inside(dataRoot, candidate)) throw new Error("Absolute fixture path must stay under freecad-source/data"); requested = path.relative(dataRoot, candidate).split(path.sep).join("/"); } requested = requested.replace(/^(?:working\/FreeCAD\/)?freecad-source\/data\//i, "").replace(/^data\//i, ""); if (requested.includes("..") || requested.startsWith("/")) throw new Error("Fixture path must stay under freecad-source/data"); const allowed = new Set(extensions.map(extension => extension.toLowerCase())); const requestedBase = path.basename(requested); const requestedExt = path.extname(requestedBase).toLowerCase(); const requestedStem = requestedExt ? requestedBase.slice(0, -requestedExt.length) : requestedBase; if (requestedExt && !allowed.has(requestedExt)) throw new Error(`Unsupported fixture extension ${requestedExt}; expected ${[...allowed].join(", ")}`); const areas = requested.includes("/") ? [path.dirname(requested)] : OFFICIAL_FIXTURE_AREAS; const exact = [], folded = []; for (const area of areas) { const directory = path.resolve(resolveDirectoryInsensitive(dataRoot, area)); if (!inside(dataRoot, directory)) continue; const names = filesIn(directory); for (const name of names) { if (path.extname(name).toLowerCase() && !allowed.has(path.extname(name).toLowerCase())) continue; const relative = path.posix.join(area.replaceAll(path.sep, "/"), name); const requestedRelative = requested.includes("/") ? requested : name; const stem = name.slice(0, -path.extname(name).length); const basenameMatch = requestedExt ? name === requestedBase : stem === requestedStem; const foldedBasenameMatch = requestedExt ? name.toLowerCase() === requestedBase.toLowerCase() : stem.toLowerCase() === requestedStem.toLowerCase(); if (relative === requestedRelative || basenameMatch) exact.push(path.join(directory, name)); if (relative.toLowerCase() === requested.toLowerCase() || foldedBasenameMatch) folded.push(path.join(directory, name)); } } const matches = [...new Set(exact.length ? exact : folded)]; if (matches.length === 1) return matches[0]; if (matches.length > 1) throw new Error(`Fixture is ambiguous: ${input}; candidates: ${matches.map(file => path.relative(root, file)).join(", ")}`); const searched = [...new Set(areas.map(area => path.relative(root, path.resolve(dataRoot, area))))]; const suggestions = [...new Set(areas.flatMap(area => filesIn(path.resolve(dataRoot, area)).filter(name => name.toLowerCase().includes(requestedBase.toLowerCase())).map(name => path.posix.join(area, name))))].slice(0, 8); throw new Error(`Fixture not found: ${input}; searched: ${searched.join(", ")}${suggestions.length ? `; candidates: ${suggestions.join(", ")}` : ""}`); } export function officialFixtureCaseId(root, fixture) { const relative = path.relative(root, fixture).split(path.sep).join("/"); const stem = path.basename(fixture, path.extname(fixture)); if (relative.includes("/data/examples/")) return `fixture.examples.${stem}`; if (relative.includes("/data/tests/ModelRefineTests/")) return `fixture.tests.ModelRefineTests.${stem}`; if (relative.includes("/data/tests/Step/")) return `fixture.tests.Step.${stem}`; return `fixture.tests.${stem}${path.extname(fixture).toLowerCase()}`; }