import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { processContext } from '../cli-operation.ts'; import { withIosOperationScope } from '../integrations/ios-operation-scope.ts'; import { projectRegistry } from '../integrations/projects.ts'; test.each(['iOS first', 'Android first', 'recognition-only before Android'] as const)( 'overlapping Godot plugins preserve target precedence with %s', async (order) => { const fixture = realpathSync(mkdtempSync(join(tmpdir(), 'stim-plugin-target-selection-'))); const previousHome = process.env.STIM_HOME; process.env.STIM_HOME = fixture; try { const root = join(fixture, 'game'); const plugin = join(fixture, 'ios'); const sentinel = join(fixture, 'imported'); mkdirSync(root); mkdirSync(plugin); writeFileSync(join(root, 'project.godot'), ''); writeFileSync( join(plugin, 'package.json'), JSON.stringify({ name: 'fixture-ios', version: '1.0.0', stimPlugin: { kinds: { project: 2 }, project: { id: 'fixture-ios', name: 'Fixture iOS', markers: ['project.godot'], targets: { ios: {} }, module: 'plugin.mjs', }, }, }), ); writeFileSync( join(plugin, 'plugin.mjs'), `import { writeFileSync } from 'node:fs'; writeFileSync(${JSON.stringify(sentinel)}, 'imported'); throw new Error('fixture iOS module imported'); `, ); const recognition = join(fixture, 'recognition'); mkdirSync(recognition); writeFileSync( join(recognition, 'package.json'), JSON.stringify({ name: 'fixture-recognition', version: '1.0.0', stimPlugin: { kinds: { project: 1 }, project: { id: 'fixture-recognition', name: 'Recognition Only', markers: ['project.godot'] }, }, }), ); const plugins = order === 'recognition-only before Android' ? [plugin, recognition, 'godot'] : order === 'iOS first' ? [plugin, 'godot'] : ['godot', plugin]; writeFileSync(join(fixture, 'config.json'), JSON.stringify({ plugins })); const resolved = { context: { projectPath: root, gitCommonDir: null, repoRoot: null }, settings: {} }; const android = projectRegistry.selectAndroid(root); const recognitionOnly = order === 'recognition-only before Android'; const androidProject = 'load' in android ? await android.load(resolved) : null; const expectedMessage = recognitionOnly ? `The plugin fixture-recognition recognises ${root} as a Recognition Only project and adds no build or run for it.` : 'Check the package name of the APK that the plugin @stim-cli/plugin-godot builds.'; expect('problem' in android ? android.problem.message : androidProject?.packageRemedy).toBe(expectedMessage); expect(androidProject?.variantProblem('release')).toBe(recognitionOnly ? undefined : null); const doctor = projectRegistry.selectDoctor(root, 'android'); expect(doctor.platforms).toEqual(recognitionOnly ? [] : ['android']); expect(doctor.problem?.kind).toBe(recognitionOnly ? 'not-an-app' : undefined); for (const inspection of await doctor.load()) { await inspection.inspect({ root, options: { platform: 'android' }, settings: {}, optimizations: null, platforms: ['android'], repoRoot: null, }); } expect(existsSync(sentinel)).toBe(false); expect(projectRegistry.detectProject(root, resolved).platforms).toEqual( recognitionOnly ? ['ios'] : ['ios', 'android'], ); const ios = projectRegistry.selectIos(root); if (!('load' in ios)) throw new Error(ios.problem.message); expect(ios.iosModulePackage).toBe('fixture-ios'); await expect(withIosOperationScope(processContext(root), (scope) => ios.load({}, scope))).rejects.toMatchObject({ code: 'STIM_NO_PROJECT', message: expect.stringContaining('fixture iOS module imported'), }); expect(existsSync(sentinel)).toBe(true); } finally { if (previousHome === undefined) delete process.env.STIM_HOME; else process.env.STIM_HOME = previousHome; rmSync(fixture, { recursive: true, force: true }); } }, ); test('refusing an iOS module preserves separate v1 recognition of a native Gradle root', () => { const fixture = realpathSync(mkdtempSync(join(tmpdir(), 'stim-plugin-target-selection-'))); const previousHome = process.env.STIM_HOME; process.env.STIM_HOME = fixture; try { const owner = join(fixture, 'app'); const native = join(owner, 'android'); mkdirSync(native, { recursive: true }); writeFileSync(join(owner, 'package.json'), JSON.stringify({ dependencies: { 'react-native': '0.81.0' } })); for (const name of ['settings.gradle.kts', 'gradlew', 'fixture.marker']) writeFileSync(join(native, name), ''); const plugins = ['fixture-ios', 'fixture-recognition'].map((id, index) => { const dir = join(fixture, id); mkdirSync(dir); const ios = index === 0; writeFileSync( join(dir, 'package.json'), JSON.stringify({ name: id, version: '1.0.0', stimPlugin: { kinds: { project: ios ? 2 : 1 }, project: { id, name: id, markers: ['fixture.marker'], ...(ios ? { targets: { ios: {} }, module: 'plugin.mjs' } : {}), }, }, }), ); return dir; }); writeFileSync(join(fixture, 'config.json'), JSON.stringify({ plugins })); expect(projectRegistry.findProjectRoot(native)).toBe(native); expect(projectRegistry.selectAndroid(native)).toMatchObject({ id: 'native-android' }); expect(projectRegistry.selectIos(native)).toMatchObject({ problem: { message: expect.stringContaining('The plugin fixture-ios cannot serve iOS') }, }); } finally { if (previousHome === undefined) delete process.env.STIM_HOME; else process.env.STIM_HOME = previousHome; rmSync(fixture, { recursive: true, force: true }); } });