Skip to content

Commit 9c74828

Browse files
committed
test(typestubs): stage the consumer in its own temporary project, never in node_modules
1 parent da43792 commit 9c74828

1 file changed

Lines changed: 39 additions & 39 deletions

File tree

‎tests/validate-typestubs.mjs‎

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @Email: <[email protected]>
88
* -----
99
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
10-
* @Last modified time: 2026-10-03T22:14:55-07:00 (1791090895)
10+
* @Last modified time: 2026-10-12T14:57:14-07:00 (1791842234)
1111
* -----
1212
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
1313
*
@@ -31,20 +31,19 @@
3131
* Unlike tests/validate-typescript.mjs (which runs under `--customConditions slothlet-dev` against the
3232
* in-repo declarations), this runs WITHOUT that condition, exercising exactly what a published consumer
3333
* resolves: core stub → @cldmv/slothlet-types. The satellite is staged from the real carve output
34-
* (build-subpackages.mjs) and fixtured into node_modules alongside a self-link to core, mirroring a
35-
* project that installed both packages.
34+
* (build-subpackages.mjs) into the check's own temporary project, beside a link to core, mirroring a
35+
* project that installed both packages. The project's node_modules is never written to: it can be
36+
* shared (a worktree whose node_modules links to another checkout's), and a run cut short would leave
37+
* its staging there for every other checkout to resolve.
3638
*/
3739

3840
import { execSync, execFileSync } from "node:child_process";
39-
import { writeFileSync, readFileSync, rmSync, mkdirSync, existsSync, cpSync, symlinkSync, lstatSync, renameSync } from "node:fs";
41+
import { writeFileSync, readFileSync, rmSync, mkdirSync, existsSync, cpSync, symlinkSync, lstatSync } from "node:fs";
4042
import { join, dirname } from "node:path";
4143
import { fileURLToPath, pathToFileURL } from "node:url";
4244

4345
const __dirname = dirname(fileURLToPath(import.meta.url));
4446
const projectRoot = dirname(__dirname);
45-
const nodeModules = join(projectRoot, "node_modules", "@cldmv");
46-
const satelliteLink = join(nodeModules, "slothlet-types");
47-
const coreLink = join(nodeModules, "slothlet");
4847
const carved = join(projectRoot, "dist-packages", "slothlet-types");
4948

5049
// The real, documented consumer surface (2026-09 scoping decision): the slothlet() factory itself,
@@ -119,9 +118,11 @@ const RESOLUTIONS = ["bundler", "nodenext"];
119118
function tsc(testFiles, resolution, extraFlags = "") {
120119
const moduleFlag = resolution === "nodenext" ? "nodenext" : "esnext";
121120
const files = (Array.isArray(testFiles) ? testFiles : [testFiles]).map((file) => `"${file}"`).join(" ");
121+
// --preserveSymlinks: the stubs are read through the staged link to core, so the packages they import
122+
// resolve from the staged node_modules, as they would from a consumer's install.
122123
try {
123124
const out = execSync(
124-
`npx tsc --noEmit --strict --moduleResolution ${resolution} --module ${moduleFlag} --target es2022 ${extraFlags} ${files}`,
125+
`npx tsc --noEmit --strict --preserveSymlinks --moduleResolution ${resolution} --module ${moduleFlag} --target es2022 ${extraFlags} ${files}`,
125126
{ stdio: "pipe", encoding: "utf8", cwd: projectRoot }
126127
);
127128
return { ok: true, out };
@@ -142,33 +143,23 @@ function main() {
142143
if (!existsSync(carved)) throw new Error("carve produced no dist-packages/slothlet-types");
143144

144145
const tmpDir = join(projectRoot, "tmp", `typestub-sim-${process.pid}`);
145-
const satelliteBackup = `${satelliteLink}.real-backup`;
146-
let createdCoreLink = false;
147-
let backedUpSatellite = false;
148-
mkdirSync(nodeModules, { recursive: true });
149-
mkdirSync(tmpDir, { recursive: true });
146+
const stagedModules = join(tmpDir, "node_modules", "@cldmv");
147+
const satelliteLink = join(stagedModules, "slothlet-types");
148+
const coreLink = join(stagedModules, "slothlet");
149+
mkdirSync(stagedModules, { recursive: true });
150+
// Its own package.json makes the temporary project a consumer of its own: inside this package's
151+
// folder, `@cldmv/slothlet` would otherwise resolve to the package itself, past the staged install.
152+
writeFileSync(join(tmpDir, "package.json"), JSON.stringify({ name: "typestub-consumer", private: true, type: "module" }), "utf8");
150153
const testFile = join(tmpDir, "consumer.mts");
151154
writeFileSync(testFile, CONSUMER, "utf8");
152155

153156
let failed = false;
154157
try {
155-
// A real consumer installs both packages. Self-link core so the satellite's `@cldmv/slothlet/*`
156-
// back-references (self-referencing specifiers preserved in the .d.mts) resolve. Use a junction so
157-
// the link works on Windows without Developer Mode / admin — a plain "dir" symlink needs elevation;
158-
// Node reports junctions as symlinks to lstat, so the cleanup below still removes it.
159-
if (!existsSync(coreLink)) {
160-
symlinkSync(projectRoot, coreLink, "junction");
161-
createdCoreLink = true;
162-
}
163-
164-
// Preserve a developer's real @cldmv/slothlet-types instead of destroying it: move it aside and
165-
// restore it in the finally (the satellite MUST live in node_modules for the stub's back-reference
166-
// to resolve, so this stages there but never clobbers).
167-
rmSync(satelliteBackup, { recursive: true, force: true });
168-
if (existsSync(satelliteLink)) {
169-
renameSync(satelliteLink, satelliteBackup);
170-
backedUpSatellite = true;
171-
}
158+
// A real consumer installs both packages. Link core so the satellite's `@cldmv/slothlet/*`
159+
// back-references (self-referencing specifiers preserved in the .d.mts) resolve to this project.
160+
// Use a junction so the link works on Windows without Developer Mode / admin — a plain "dir"
161+
// symlink needs elevation; Node reports junctions as symlinks to lstat.
162+
symlinkSync(projectRoot, coreLink, "junction");
172163

173164
// 1) Satellite present → consumer type-checks through the stubs, under every resolver.
174165
cpSync(carved, satelliteLink, { recursive: true });
@@ -249,17 +240,26 @@ function main() {
249240
console.log("✅ without it: TypeScript reports the missing @cldmv/slothlet-types (expected degradation)");
250241
} else {
251242
failed = true;
252-
console.error("❌ expected a 'Cannot find module @cldmv/slothlet-types' error when absent:\n" + withoutPack.out);
243+
// TypeScript looks for the package in every node_modules above the consumer, so a copy
244+
// installed above it is found and the package is not absent: name it.
245+
const installedAbove = [];
246+
for (let dir = projectRoot; ; dir = dirname(dir)) {
247+
const candidate = join(dir, "node_modules", "@cldmv", "slothlet-types");
248+
if (existsSync(candidate)) installedAbove.push(candidate);
249+
if (dirname(dir) === dir) break;
250+
}
251+
console.error(
252+
"❌ expected a 'Cannot find module @cldmv/slothlet-types' error when absent:\n" +
253+
withoutPack.out +
254+
(installedAbove.length ? `\n @cldmv/slothlet-types is installed at ${installedAbove.join(", ")}` : "")
255+
);
253256
}
254257
} finally {
255-
rmSync(satelliteLink, { recursive: true, force: true });
256-
if (backedUpSatellite) renameSync(satelliteBackup, satelliteLink); // restore the developer's real install
257-
if (createdCoreLink) {
258-
try {
259-
if (lstatSync(coreLink).isSymbolicLink()) rmSync(coreLink, { force: true });
260-
} catch {
261-
/* ignore */
262-
}
258+
// Remove the link to core on its own first, so removing the temporary project never reaches core.
259+
try {
260+
if (lstatSync(coreLink).isSymbolicLink()) rmSync(coreLink, { force: true });
261+
} catch {
262+
/* not created */
263263
}
264264
rmSync(tmpDir, { recursive: true, force: true });
265265
}

0 commit comments

Comments
 (0)