/ scripts / run-next-typegen.test.mjs
run-next-typegen.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 { describe, it } from 'node:test'
 6  
 7  import { BUILD_BOOTSTRAP_ROOT_NAME } from './build-bootstrap-env.mjs'
 8  import {
 9    TYPEGEN_ARTIFACT_PATHS,
10    buildNextTypegenEnv,
11    cleanupTypegenArtifacts,
12  } from './run-next-typegen.mjs'
13  
14  describe('run-next-typegen', () => {
15    it('forces build mode for deterministic type generation', () => {
16      const env = buildNextTypegenEnv({ FOO: 'bar' })
17      assert.equal(env.FOO, 'bar')
18      assert.equal(env.SWARMCLAW_BUILD_MODE, '1')
19      assert.equal(env.DATA_DIR?.endsWith(path.join(BUILD_BOOTSTRAP_ROOT_NAME, 'data')), true)
20      assert.equal(env.WORKSPACE_DIR?.endsWith(path.join(BUILD_BOOTSTRAP_ROOT_NAME, 'workspace')), true)
21      assert.equal(
22        env.BROWSER_PROFILES_DIR?.endsWith(path.join(BUILD_BOOTSTRAP_ROOT_NAME, 'browser-profiles')),
23        true,
24      )
25    })
26  
27    it('preserves an explicit SWARMCLAW_BUILD_MODE value', () => {
28      const env = buildNextTypegenEnv({ SWARMCLAW_BUILD_MODE: 'custom' })
29      assert.equal(env.SWARMCLAW_BUILD_MODE, 'custom')
30    })
31  
32    it('removes stale typegen artifacts before running next typegen', () => {
33      const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'swarmclaw-typegen-'))
34      try {
35        for (const relativePath of TYPEGEN_ARTIFACT_PATHS) {
36          const absolutePath = path.join(tempDir, relativePath)
37          fs.mkdirSync(path.dirname(absolutePath), { recursive: true })
38          if (relativePath.endsWith('.tsbuildinfo')) {
39            fs.writeFileSync(absolutePath, 'stale')
40          } else {
41            fs.mkdirSync(absolutePath, { recursive: true })
42            fs.writeFileSync(path.join(absolutePath, 'stale.ts'), 'export {}')
43          }
44        }
45  
46        cleanupTypegenArtifacts(tempDir)
47  
48        for (const relativePath of TYPEGEN_ARTIFACT_PATHS) {
49          assert.equal(fs.existsSync(path.join(tempDir, relativePath)), false)
50        }
51      } finally {
52        fs.rmSync(tempDir, { recursive: true, force: true })
53      }
54    })
55  })