|
| 1 | +import type {InputCapabilityContext} from '../plugins/plugin-core' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as os from 'node:os' |
| 4 | +import * as path from 'node:path' |
| 5 | +import glob from 'fast-glob' |
| 6 | +import {describe, expect, it, vi} from 'vitest' |
| 7 | +import {mergeConfig} from '../config' |
| 8 | +import {AindexInputCapability} from './input-aindex' |
| 9 | + |
| 10 | +function createLoggerMock(): { |
| 11 | + readonly logger: InputCapabilityContext['logger'] |
| 12 | + readonly warn: ReturnType<typeof vi.fn> |
| 13 | +} { |
| 14 | + const warn = vi.fn() |
| 15 | + |
| 16 | + return { |
| 17 | + logger: { |
| 18 | + error: vi.fn(), |
| 19 | + warn, |
| 20 | + info: vi.fn(), |
| 21 | + debug: vi.fn(), |
| 22 | + trace: vi.fn(), |
| 23 | + fatal: vi.fn() |
| 24 | + }, |
| 25 | + warn |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function createContext( |
| 30 | + tempWorkspace: string, |
| 31 | + logger: InputCapabilityContext['logger'] |
| 32 | +): InputCapabilityContext { |
| 33 | + return { |
| 34 | + logger, |
| 35 | + fs, |
| 36 | + path, |
| 37 | + glob, |
| 38 | + userConfigOptions: mergeConfig({workspaceDir: tempWorkspace}), |
| 39 | + dependencyContext: {} |
| 40 | + } as InputCapabilityContext |
| 41 | +} |
| 42 | + |
| 43 | +function createAindexProject(tempWorkspace: string, projectName: string): { |
| 44 | + readonly configDir: string |
| 45 | +} { |
| 46 | + const distProjectDir = path.join(tempWorkspace, 'aindex', 'dist', 'app', projectName) |
| 47 | + const configDir = path.join(tempWorkspace, 'aindex', 'app', projectName) |
| 48 | + |
| 49 | + fs.mkdirSync(distProjectDir, {recursive: true}) |
| 50 | + fs.mkdirSync(configDir, {recursive: true}) |
| 51 | + |
| 52 | + return {configDir} |
| 53 | +} |
| 54 | + |
| 55 | +describe('aindex input capability project config loading', () => { |
| 56 | + it('loads project.json5 using JSON5 features without any jsonc fallback', () => { |
| 57 | + const tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'tnmsc-aindex-project-json5-')) |
| 58 | + const {logger, warn} = createLoggerMock() |
| 59 | + |
| 60 | + try { |
| 61 | + const {configDir} = createAindexProject(tempWorkspace, 'project-a') |
| 62 | + fs.writeFileSync(path.join(configDir, 'project.json5'), [ |
| 63 | + '{', |
| 64 | + ' // JSON5 comment support', |
| 65 | + ' includeSeries: [\'alpha\'],', |
| 66 | + ' subSeries: {', |
| 67 | + ' skills: [\'ship-*\'],', |
| 68 | + ' },', |
| 69 | + '}', |
| 70 | + '' |
| 71 | + ].join('\n'), 'utf8') |
| 72 | + |
| 73 | + const result = new AindexInputCapability().collect(createContext(tempWorkspace, logger)) |
| 74 | + const project = result.workspace?.projects[0] |
| 75 | + |
| 76 | + expect(project?.name).toBe('project-a') |
| 77 | + expect(project?.projectConfig).toEqual({ |
| 78 | + includeSeries: ['alpha'], |
| 79 | + subSeries: { |
| 80 | + skills: ['ship-*'] |
| 81 | + } |
| 82 | + }) |
| 83 | + expect(warn).not.toHaveBeenCalled() |
| 84 | + } |
| 85 | + finally { |
| 86 | + fs.rmSync(tempWorkspace, {recursive: true, force: true}) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + it('ignores legacy project.jsonc after the hard cut', () => { |
| 91 | + const tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'tnmsc-aindex-project-jsonc-legacy-')) |
| 92 | + const {logger, warn} = createLoggerMock() |
| 93 | + |
| 94 | + try { |
| 95 | + const {configDir} = createAindexProject(tempWorkspace, 'project-b') |
| 96 | + fs.writeFileSync(path.join(configDir, 'project.jsonc'), '{"includeSeries":["legacy"]}\n', 'utf8') |
| 97 | + |
| 98 | + const result = new AindexInputCapability().collect(createContext(tempWorkspace, logger)) |
| 99 | + const project = result.workspace?.projects[0] |
| 100 | + |
| 101 | + expect(project?.name).toBe('project-b') |
| 102 | + expect(project?.projectConfig).toBeUndefined() |
| 103 | + expect(warn).not.toHaveBeenCalled() |
| 104 | + } |
| 105 | + finally { |
| 106 | + fs.rmSync(tempWorkspace, {recursive: true, force: true}) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + it('emits JSON5 diagnostics for invalid project.json5 syntax', () => { |
| 111 | + const tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'tnmsc-aindex-project-json5-invalid-')) |
| 112 | + const {logger, warn} = createLoggerMock() |
| 113 | + |
| 114 | + try { |
| 115 | + const {configDir} = createAindexProject(tempWorkspace, 'project-c') |
| 116 | + fs.writeFileSync(path.join(configDir, 'project.json5'), '{includeSeries: [\'broken\',]} trailing', 'utf8') |
| 117 | + |
| 118 | + const result = new AindexInputCapability().collect(createContext(tempWorkspace, logger)) |
| 119 | + const project = result.workspace?.projects[0] |
| 120 | + const diagnostic = warn.mock.calls[0]?.[0] |
| 121 | + |
| 122 | + expect(project?.name).toBe('project-c') |
| 123 | + expect(project?.projectConfig).toBeUndefined() |
| 124 | + expect(warn).toHaveBeenCalledTimes(1) |
| 125 | + expect(diagnostic).toEqual(expect.objectContaining({ |
| 126 | + code: 'AINDEX_PROJECT_JSON5_INVALID', |
| 127 | + title: 'Failed to parse project.json5 for project-c', |
| 128 | + exactFix: ['Fix the JSON5 syntax in project.json5 and rerun tnmsc.'] |
| 129 | + })) |
| 130 | + } |
| 131 | + finally { |
| 132 | + fs.rmSync(tempWorkspace, {recursive: true, force: true}) |
| 133 | + } |
| 134 | + }) |
| 135 | +}) |
0 commit comments