Files
rea/scripts/verify-windows-native-inspection.mjs
T
N0zoM1z0morlutofactory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>Kaoru0822
008c55c835 test(ci): verify native inspection ownership after directory rename
The native directory lease now shares DELETE as intended by #2445, so rename
is permitted. Verify retained identity after rename and that retry/teardown
remove the owned object while preserving a replacement at its former path.
Keep the injected first-disposition failure, recovery, admission-cache and
environment-cleanup checks. No native runtime behavior is changed.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-10-14 02:40:31 +08:00

113 lines
4.6 KiB
JavaScript

import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import {
copyFile,
mkdir,
mkdtemp,
readFile,
readdir,
rm,
stat,
symlink,
writeFile,
} from "node:fs/promises";
import { createRequire } from "node:module";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
if (process.platform !== "win32")
throw new Error("Native inspection ownership requires real Windows NTFS");
const root = resolve(process.argv[2] ?? ".");
const artifact = resolve(
process.argv[3] ?? ".cache/windows-native-inspection-fixture",
);
const workspace = await mkdtemp(join(tmpdir(), "rea-inspection-conformance-"));
try {
// Isolate the fixture from the packaged production artifact and loader cache.
await mkdir(join(workspace, "dist", "windows"), { recursive: true });
await mkdir(join(workspace, "native", "windows", "build"), {
recursive: true,
});
await writeFile(join(workspace, "package.json"), '{"type":"module"}');
await copyFile(
join(root, "dist", "windows", "WindowsNativeLoader.js"),
join(workspace, "dist", "windows", "WindowsNativeLoader.js"),
);
await copyFile(
join(root, "dist", "generatedPackageMetadata.js"),
join(workspace, "dist", "generatedPackageMetadata.js"),
);
for (const name of ["manifest.json", "rea-windows-x64.node"])
await copyFile(
join(artifact, name),
join(workspace, "native", "windows", "build", name),
);
const require = createRequire(join(root, "package.json"));
await mkdir(join(workspace, "node_modules"));
await symlink(
dirname(require.resolve("zod/package.json")),
join(workspace, "node_modules", "zod"),
"junction",
);
const loader = pathToFileURL(
join(workspace, "dist", "windows", "WindowsNativeLoader.js"),
).href;
const source = `import assert from 'node:assert/strict';
import {existsSync,mkdirSync,readFileSync,readdirSync,renameSync,statSync,writeFileSync} from 'node:fs';
import {join} from 'node:path';
import {systemWindowsNativeAuthority} from ${JSON.stringify(loader)};
const temp=process.env.TEMP;
const first=systemWindowsNativeAuthority();
assert.equal(first.available,false);
assert.equal(first.retryable,true);
assert.match(first.reason,/Delete owned object by handle failed.*Win32 5/s);
const [name,...extra]=readdirSync(temp);assert.ok(name);assert.equal(extra.length,0);
const path=join(temp,name);const identity=statSync(path,{bigint:true});
const retainedPath=path+'-retained';renameSync(path,retainedPath);
assert.equal(statSync(retainedPath,{bigint:true}).ino,identity.ino);
// FILE_SHARE_DELETE permits rename; ownership follows the retained handle.
mkdirSync(path);const replacement=statSync(path,{bigint:true});
const marker=join(path,'caller-evidence.txt');writeFileSync(marker,'caller-owned replacement');
if(process.argv[2]!=='teardown'){
const second=systemWindowsNativeAuthority();assert.equal(second.available,true);
assert.equal(existsSync(retainedPath),false);
assert.deepEqual(readdirSync(temp),[name]);
assert.equal(statSync(path,{bigint:true}).ino,replacement.ino);
assert.equal(readFileSync(marker,'utf8'),'caller-owned replacement');
assert.equal(systemWindowsNativeAuthority(),second);
}
console.log(JSON.stringify({mode:process.argv[2],firstFailed:true,retainedOwner:true,inode:identity.ino.toString(),replacementName:name,replacementInode:replacement.ino.toString()}));
`;
const fixture = join(workspace, "probe.mjs");
await writeFile(fixture, source);
for (const mode of ["retry", "teardown"]) {
const temp = join(workspace, mode);
await mkdir(temp);
const observed = JSON.parse(
execFileSync(process.execPath, [fixture, mode], {
encoding: "utf8",
timeout: 20_000,
windowsHide: true,
env: { ...process.env, TEMP: temp, TMP: temp },
}),
);
assert.equal(observed.retainedOwner, true);
assert.deepEqual(await readdir(temp), [observed.replacementName]);
const replacement = join(temp, observed.replacementName);
assert.equal(
(await stat(replacement, { bigint: true })).ino.toString(),
observed.replacementInode,
);
assert.equal(
await readFile(join(replacement, "caller-evidence.txt"), "utf8"),
"caller-owned replacement",
);
}
process.stdout.write(
`${JSON.stringify({ ok: true, controlledDispositionFailure: true, retainedOwner: true, renamePreservesIdentity: true, foreignReplacementPreserved: true, retryRecovers: true, successfulAdmissionCached: true, environmentTeardownCleansProbe: true, issue1701HostCauseReproduced: false })}\n`,
);
} finally {
await rm(workspace, { recursive: true, force: true });
}