Files
orca/config/scripts/script-module-dependencies.test.mjs
T
Neil 2a73e6b03a fix(windows): say why the packaged ConPTY fell back, not just that it did
The previous commit resolved the addon by architecture but still had one message
for every way the resolution could land on the published prebuild. Those ways
want opposite remedies, and the one it printed was the remedy the commit before
it had just called wrong:

- no source build in the package at all — the slice has to be built somewhere
  that can build node-pty for the target arch.
- a source build that is there but is the packaging host's architecture, because
  the cross-arch rebuild did not honour `--arch` — re-running that rebuild is the
  fix, and "package on a Windows arm64 host" is neither necessary nor possible.

The second is the common one, since node-pty publishes a prebuild for both
Windows arches and prune keeps the target's on every cross-arch package. So the
old text fired mostly on the case it described least. It now reports which source
builds were skipped and the machine field each carried, and names the rebuild
command.

"Nothing the target can load" had the same problem in reverse: a zero-length or
truncated `conpty.node` got a cross-architecture diagnosis. Every candidate is
now named with what was actually read, including "not a PE image".

The rebuild path asserts the architecture too. A rebuild that ignored `--arch`
was otherwise only visible at packaging, two steps from the command that fixes
it. Arches with no known machine value are left unjudged rather than guessed at.

Two things the extraction broke or nearly broke, both found by mutation:

- the shared PE reader answers `null` where the relay builder's private copy
  returned a number, which would have turned its "node-gyp ignored --arch" error
  into a `TypeError`. Both callers now go through `describePeMachine`.
- the rebuild fixtures stage a script's co-located modules by walking its
  imports, and the walker only understood `from '...'` — so the gate's new
  `require('./windows-pe-machine.cjs')` was left behind and every subprocess test
  failed with a resolution error, which is the exact failure its own comment
  warns about. It now follows `require` and bare side-effect `import` as well,
  and has tests; the fixture stages the gate by walking it rather than by naming
  one file.

Fixtures write real PE headers through one shared builder instead of three
hand-rolled ones.
2026-09-15 22:19:46 -07:00

101 lines
3.7 KiB
JavaScript

import { existsSync, mkdtempSync, readdirSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { copyScriptWithLocalModules } from './script-module-dependencies.mjs'
const fixtureDir = mkdtempSync(join(tmpdir(), 'script-module-dependencies-'))
function sourceTree(files) {
const sourceDir = mkdtempSync(join(fixtureDir, 'source-'))
for (const [name, contents] of Object.entries(files)) {
writeFileSync(join(sourceDir, name), contents)
}
return sourceDir
}
function copiedNames(files, entryName) {
const sourceDir = sourceTree(files)
const destinationDir = join(mkdtempSync(join(fixtureDir, 'dest-')), 'scripts')
copyScriptWithLocalModules(join(sourceDir, entryName), destinationDir)
return readdirSync(destinationDir).sort()
}
describe('copyScriptWithLocalModules', () => {
it('takes the entry script itself', () => {
expect(copiedNames({ 'entry.mjs': 'export const a = 1\n' }, 'entry.mjs')).toEqual(['entry.mjs'])
})
it('follows a co-located import', () => {
expect(
copiedNames(
{ 'entry.mjs': "import { a } from './dep.mjs'\n", 'dep.mjs': 'export const a = 1\n' },
'entry.mjs'
)
).toEqual(['dep.mjs', 'entry.mjs'])
})
// The Windows addon gates are .cjs and reach each other by require. A module
// pulled in only that way used to be left behind, and the subprocess then
// failed with a resolution error that looks nothing like the defect it hides.
it('follows a co-located require, not only an import', () => {
expect(
copiedNames(
{
'entry.cjs': "const { a } = require('./dep.cjs')\nmodule.exports = { a }\n",
'dep.cjs': 'module.exports = { a: 1 }\n'
},
'entry.cjs'
)
).toEqual(['dep.cjs', 'entry.cjs'])
})
it('follows a require reached only through an imported module', () => {
const names = copiedNames(
{
'entry.mjs': "import './middle.cjs'\n",
'middle.cjs': "require('./leaf.cjs')\n",
'leaf.cjs': 'module.exports = {}\n'
},
'entry.mjs'
)
expect(names).toContain('leaf.cjs')
})
it('leaves package and builtin specifiers alone', () => {
const sourceDir = sourceTree({
'entry.mjs': "import { join } from 'node:path'\nimport x from 'some-package'\n"
})
const destinationDir = join(mkdtempSync(join(fixtureDir, 'dest-')), 'scripts')
copyScriptWithLocalModules(join(sourceDir, 'entry.mjs'), destinationDir)
expect(readdirSync(destinationDir)).toEqual(['entry.mjs'])
})
it('terminates on a cycle rather than recursing forever', () => {
expect(
copiedNames({ 'a.mjs': "import './b.mjs'\n", 'b.mjs': "import './a.mjs'\n" }, 'a.mjs')
).toEqual(['a.mjs', 'b.mjs'])
})
it('creates the destination directory it was handed', () => {
const sourceDir = sourceTree({ 'entry.mjs': 'export const a = 1\n' })
const destinationDir = join(mkdtempSync(join(fixtureDir, 'dest-')), 'nested', 'scripts')
copyScriptWithLocalModules(join(sourceDir, 'entry.mjs'), destinationDir)
expect(existsSync(join(destinationDir, 'entry.mjs'))).toBe(true)
})
// The real tree this stages: the packaged-addon gate reaches its PE reader by
// require, so a walker that missed it would break every rebuild fixture.
it('stages the node-pty job-ownership gate with everything it requires', () => {
const destinationDir = join(mkdtempSync(join(fixtureDir, 'dest-')), 'scripts')
copyScriptWithLocalModules(
new URL('./node-pty-job-ownership.cjs', import.meta.url).pathname,
destinationDir
)
expect(readdirSync(destinationDir).sort()).toEqual([
'node-pty-job-ownership.cjs',
'windows-pe-machine.cjs'
])
})
})