/ scripts / postinstall.test.mjs
postinstall.test.mjs
 1  import assert from 'node:assert/strict'
 2  import fs from 'node:fs'
 3  import os from 'node:os'
 4  import path from 'node:path'
 5  import { spawnSync } from 'node:child_process'
 6  import { fileURLToPath } from 'node:url'
 7  import { describe, it } from 'node:test'
 8  
 9  const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
10  
11  describe('postinstall', () => {
12    it('skips sandbox browser setup when the helper script is missing from the install context', () => {
13      const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'swarmclaw-postinstall-'))
14      const scriptsDir = path.join(tempDir, 'scripts')
15      fs.mkdirSync(scriptsDir, { recursive: true })
16      fs.copyFileSync(
17        path.join(repoRoot, 'scripts', 'postinstall.mjs'),
18        path.join(scriptsDir, 'postinstall.mjs'),
19      )
20      fs.writeFileSync(
21        path.join(tempDir, 'package.json'),
22        JSON.stringify({
23          name: '@swarmclawai/swarmclaw-postinstall-test',
24          version: '0.0.0-test',
25        }, null, 2),
26        'utf8',
27      )
28  
29      try {
30        const result = spawnSync(process.execPath, ['scripts/postinstall.mjs'], {
31          cwd: tempDir,
32          env: {
33            ...process.env,
34            CI: '',
35            npm_config_user_agent: 'npm/10.0.0 node/v22.0.0 darwin x64',
36          },
37          encoding: 'utf8',
38        })
39  
40        const combinedOutput = `${result.stdout || ''}\n${result.stderr || ''}`
41        assert.equal(result.status, 0, combinedOutput || 'postinstall subprocess failed')
42        assert.match(
43          combinedOutput,
44          /Sandbox browser image helper is not present in this install context\. Skipping setup\./,
45        )
46        assert.doesNotMatch(combinedOutput, /Cannot find module/)
47        assert.doesNotMatch(combinedOutput, /sandbox browser image setup failed/i)
48      } finally {
49        fs.rmSync(tempDir, { recursive: true, force: true })
50      }
51    })
52  })