|
| 1 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { |
| 6 | + TURBO_EXTENDS, |
| 7 | + packageOwnRootAnchoredInputs, |
| 8 | + packageRootAnchoredInputs, |
| 9 | + packageTaskInputs, |
| 10 | + repoRoot, |
| 11 | + type TurboTasks, |
| 12 | +} from './helpers/turbo-inputs'; |
| 13 | + |
| 14 | +/** |
| 15 | + * objectstack#29361 — the turbo-input guards read the inputs turbo resolves |
| 16 | + * for the package a row judges, not the root task's list alone. These pin the |
| 17 | + * reader against fixtures for each of turbo's three resolutions, and against |
| 18 | + * the live tree for the one package that carries its own turbo.json today. |
| 19 | + */ |
| 20 | + |
| 21 | +const ROOT: TurboTasks = { |
| 22 | + tasks: { |
| 23 | + build: { inputs: ['$TURBO_DEFAULT$', '$TURBO_ROOT$/scripts/shared.ts'] }, |
| 24 | + 'type-check': { inputs: ['$TURBO_DEFAULT$', '$TURBO_ROOT$/tsconfig.json'] }, |
| 25 | + }, |
| 26 | +}; |
| 27 | +const OWN_ONLY = '$TURBO_ROOT$/packages/other/src/only-here.ts'; |
| 28 | + |
| 29 | +const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'turbo-inputs-package-config-')); |
| 30 | +afterAll(() => fs.rmSync(scratch, { recursive: true, force: true })); |
| 31 | + |
| 32 | +/** A scratch package directory holding a `package.json` with this name. */ |
| 33 | +function scratchPackage(name: string): string { |
| 34 | + const dir = fs.mkdtempSync(path.join(scratch, 'pkg-')); |
| 35 | + fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name })); |
| 36 | + return dir; |
| 37 | +} |
| 38 | + |
| 39 | +describe('packageTaskInputs: turbo resolves a package task through its own turbo.json', () => { |
| 40 | + it('a package with no turbo.json reads the root task list unchanged', () => { |
| 41 | + const dir = scratchPackage('@fixture/plain'); |
| 42 | + expect(packageTaskInputs('build', dir, ROOT, null)).toEqual(ROOT.tasks!.build.inputs); |
| 43 | + }); |
| 44 | + |
| 45 | + it('a package task that declares no inputs inherits the root task list', () => { |
| 46 | + const dir = scratchPackage('@fixture/outputs-only'); |
| 47 | + const own: TurboTasks = { tasks: { build: {} } }; |
| 48 | + expect(packageTaskInputs('build', dir, ROOT, own)).toEqual(ROOT.tasks!.build.inputs); |
| 49 | + }); |
| 50 | + |
| 51 | + it('an input list holding $TURBO_EXTENDS$ is the root list plus the package entries', () => { |
| 52 | + const dir = scratchPackage('@fixture/extends'); |
| 53 | + const own: TurboTasks = { tasks: { build: { inputs: [TURBO_EXTENDS, OWN_ONLY] } } }; |
| 54 | + expect(packageTaskInputs('build', dir, ROOT, own)).toEqual([ |
| 55 | + '$TURBO_DEFAULT$', |
| 56 | + '$TURBO_ROOT$/scripts/shared.ts', |
| 57 | + OWN_ONLY, |
| 58 | + ]); |
| 59 | + expect(packageRootAnchoredInputs('build', dir, ROOT, own)).toEqual([ |
| 60 | + 'scripts/shared.ts', |
| 61 | + 'packages/other/src/only-here.ts', |
| 62 | + ]); |
| 63 | + expect(packageOwnRootAnchoredInputs('build', dir, own)).toEqual(['packages/other/src/only-here.ts']); |
| 64 | + }); |
| 65 | + |
| 66 | + it('the marker extends wherever it sits in the list, as turbo reads it', () => { |
| 67 | + const dir = scratchPackage('@fixture/extends-last'); |
| 68 | + const own: TurboTasks = { tasks: { build: { inputs: [OWN_ONLY, TURBO_EXTENDS] } } }; |
| 69 | + expect(packageRootAnchoredInputs('build', dir, ROOT, own)).toEqual([ |
| 70 | + 'scripts/shared.ts', |
| 71 | + 'packages/other/src/only-here.ts', |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('an input list without the marker replaces the root list: the root entries stop counting', () => { |
| 76 | + const dir = scratchPackage('@fixture/replaces'); |
| 77 | + const own: TurboTasks = { tasks: { build: { inputs: [OWN_ONLY] } } }; |
| 78 | + expect(packageTaskInputs('build', dir, ROOT, own)).toEqual([OWN_ONLY]); |
| 79 | + expect(packageRootAnchoredInputs('build', dir, ROOT, own)).not.toContain('scripts/shared.ts'); |
| 80 | + }); |
| 81 | + |
| 82 | + it("one package's turbo.json leaves another package's row and its other tasks alone", () => { |
| 83 | + const own: TurboTasks = { tasks: { build: { inputs: [TURBO_EXTENDS, OWN_ONLY] } } }; |
| 84 | + const withOwn = scratchPackage('@fixture/with-own'); |
| 85 | + const other = scratchPackage('@fixture/other'); |
| 86 | + expect(packageRootAnchoredInputs('build', other, ROOT, null)).toEqual(['scripts/shared.ts']); |
| 87 | + expect(packageRootAnchoredInputs('type-check', withOwn, ROOT, own)).toEqual(['tsconfig.json']); |
| 88 | + }); |
| 89 | + |
| 90 | + it('an empty replacing list throws instead of reading as a list that covers nothing', () => { |
| 91 | + const dir = scratchPackage('@fixture/empty'); |
| 92 | + const own: TurboTasks = { tasks: { build: { inputs: [] } } }; |
| 93 | + expect(() => packageTaskInputs('build', dir, ROOT, own)).toThrow(/empty `inputs` list/); |
| 94 | + }); |
| 95 | + |
| 96 | + it('a root <package>#<task> override throws: the reader does not resolve that shape', () => { |
| 97 | + const dir = scratchPackage('@fixture/overridden'); |
| 98 | + const root: TurboTasks = { |
| 99 | + tasks: { ...ROOT.tasks, '@fixture/overridden#build': { inputs: ['$TURBO_DEFAULT$'] } }, |
| 100 | + }; |
| 101 | + expect(() => packageTaskInputs('build', dir, root, null)).toThrow(/does not resolve a root/); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('the live tree: the Service Worker generator hashes into the Console tasks only', () => { |
| 106 | + const GENERATOR = 'packages/mobile/src/serviceWorkerSource.ts'; |
| 107 | + const consoleDir = path.join(repoRoot, 'apps/console'); |
| 108 | + const coreDir = path.join(repoRoot, 'packages/core'); |
| 109 | + |
| 110 | + it.each(['build', 'type-check'])( |
| 111 | + "@object-ui/console's %s reads the root list plus the generator, narrowed to this tree", |
| 112 | + (task) => { |
| 113 | + const resolved = packageRootAnchoredInputs(task, consoleDir); |
| 114 | + expect(resolved).toContain(GENERATOR); |
| 115 | + expect(resolved).toContain('scripts/vite-*.ts'); |
| 116 | + expect(packageTaskInputs(task, consoleDir)).toContain('$TURBO_DEFAULT$'); |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + it.each(['build', 'type-check'])("another package's %s does not hash the generator", (task) => { |
| 121 | + expect(packageRootAnchoredInputs(task, coreDir)).not.toContain(GENERATOR); |
| 122 | + }); |
| 123 | + |
| 124 | + it("every package's test still hashes it: the root ui vitest config loads the Console's vite.config.ts", () => { |
| 125 | + expect(packageRootAnchoredInputs('test', coreDir)).toContain(GENERATOR); |
| 126 | + expect(packageRootAnchoredInputs('test', consoleDir)).toContain(GENERATOR); |
| 127 | + }); |
| 128 | +}); |
0 commit comments