|
| 1 | +/** |
| 2 | + * Copyright 2026 Arm Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, beforeEach } from '@jest/globals'; |
| 18 | +import * as vscode from 'vscode'; |
| 19 | +import { MANAGE_COMPONENTS_PACKS_COMMAND_ID, MERGE_FILE_COMMAND_ID, RUN_GENERATOR_COMMAND_ID } from '../manifest'; |
| 20 | +import { ProblemDiagnosticActionResolver, ProblemDiagnosticActionContext } from './problem-diagnostic-action-resolver'; |
| 21 | + |
| 22 | +const diagnosticFilePath = '/work/app.csolution.yml'; |
| 23 | + |
| 24 | +function makeContext(overrides: Partial<ProblemDiagnosticActionContext> = {}): ProblemDiagnosticActionContext { |
| 25 | + return { |
| 26 | + message: '', |
| 27 | + diagnosticFilePath, |
| 28 | + hasLocation: false, |
| 29 | + ...overrides, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function decodeCodeTarget(resolver: ProblemDiagnosticActionResolver, context: ProblemDiagnosticActionContext) { |
| 34 | + const result = resolver.resolve(context); |
| 35 | + const code = result?.code as { value: string; target: vscode.Uri } | undefined; |
| 36 | + const [command, encodedArgs] = code?.target.toString().split('?') ?? []; |
| 37 | + const args = encodedArgs ? JSON.parse(decodeURIComponent(encodedArgs)) : undefined; |
| 38 | + return { result, code, command, args }; |
| 39 | +} |
| 40 | + |
| 41 | +describe('ProblemDiagnosticActionResolver', () => { |
| 42 | + let resolver: ProblemDiagnosticActionResolver; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + resolver = new ProblemDiagnosticActionResolver(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('when no pattern matches', () => { |
| 49 | + it('returns undefined for a message with no recognizable pattern', () => { |
| 50 | + const result = resolver.resolve(makeContext({ message: 'build completed successfully' })); |
| 51 | + |
| 52 | + expect(result).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns undefined for an empty message', () => { |
| 56 | + const result = resolver.resolve(makeContext({ message: '' })); |
| 57 | + |
| 58 | + expect(result).toBeUndefined(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('merge action', () => { |
| 63 | + it('is returned even when hasLocation is true', () => { |
| 64 | + const result = resolver.resolve(makeContext({ |
| 65 | + message: "update required for file '/packs/Component/config.c'", |
| 66 | + hasLocation: true, |
| 67 | + })); |
| 68 | + |
| 69 | + expect((result?.code as { value: string } | undefined)?.value).toBe('Open in Merge View'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('encodes the absolute local path as the merge command argument', () => { |
| 73 | + const { command, args } = decodeCodeTarget(resolver, makeContext({ |
| 74 | + message: "update required for file '/packs/Component/config.c'", |
| 75 | + })); |
| 76 | + |
| 77 | + expect(command).toBe(`command:${MERGE_FILE_COMMAND_ID}`); |
| 78 | + expect(args).toEqual(['/packs/Component/config.c']); |
| 79 | + }); |
| 80 | + |
| 81 | + it('falls back to the diagnostic file path when the extracted path is relative', () => { |
| 82 | + const { args } = decodeCodeTarget(resolver, makeContext({ |
| 83 | + message: "update required for file 'relative-config.c'", |
| 84 | + diagnosticFilePath: '/work/app.csolution.yml', |
| 85 | + })); |
| 86 | + |
| 87 | + expect(args).toEqual(['/work/app.csolution.yml']); |
| 88 | + }); |
| 89 | + |
| 90 | + it('treats Windows-style C:/ paths as absolute', () => { |
| 91 | + const { args } = decodeCodeTarget(resolver, makeContext({ |
| 92 | + message: "update required for file 'C:/CubeMX/RTE/CMSIS/RTX_Config.c' from component 'Arm::Device@2.3.4'", |
| 93 | + diagnosticFilePath, |
| 94 | + })); |
| 95 | + |
| 96 | + expect(args).toEqual(['C:/CubeMX/RTE/CMSIS/RTX_Config.c']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('ignores trailing content after a semicolon in multiline merge messages', () => { |
| 100 | + const result = resolver.resolve(makeContext({ |
| 101 | + message: "update recommended for file 'C:/CubeMX/RTX_Config.c' from component 'CMSIS:RTOS2:Keil RTX5&Source'.\nMerge content from update file, rename update file to base file and remove previous base file", |
| 102 | + })); |
| 103 | + |
| 104 | + expect(result?.message).toBe("update recommended for config file 'RTX_Config.c' from component 'CMSIS:RTOS2:Keil RTX5&Source'."); |
| 105 | + }); |
| 106 | + |
| 107 | + it('produces a message with component display name when component is present', () => { |
| 108 | + const result = resolver.resolve(makeContext({ |
| 109 | + message: "update required for file '/packs/Component/config.c' from component 'Arm::Device@2.3.4'", |
| 110 | + })); |
| 111 | + |
| 112 | + expect(result?.message).toBe("update required for config file 'config.c' from component 'Device'."); |
| 113 | + }); |
| 114 | + |
| 115 | + it('strips vendor prefix from component name in the message', () => { |
| 116 | + const result = resolver.resolve(makeContext({ |
| 117 | + message: "update required for file '/packs/CMSIS/RTX_Config.c' from component 'ARM::CMSIS:RTOS2:Keil RTX5&Source'", |
| 118 | + })); |
| 119 | + |
| 120 | + expect(result?.message).toMatch(/from component 'CMSIS:RTOS2:Keil RTX5&Source'\./); |
| 121 | + }); |
| 122 | + |
| 123 | + it('uses "has a new version available for merge" message when no component is present', () => { |
| 124 | + const result = resolver.resolve(makeContext({ |
| 125 | + message: "update required for file '/packs/Component/config.c'", |
| 126 | + diagnosticFilePath, |
| 127 | + })); |
| 128 | + |
| 129 | + expect(result?.message).toBe("update required for config file 'config.c' has a new version available for merge."); |
| 130 | + }); |
| 131 | + |
| 132 | + it.each(['required', 'recommended', 'suggested'] as const)( |
| 133 | + 'produces a message with update level "%s" when no component is present', |
| 134 | + updateLevel => { |
| 135 | + const result = resolver.resolve(makeContext({ |
| 136 | + message: `update ${updateLevel} for file '/packs/Component/config.c'`, |
| 137 | + })); |
| 138 | + |
| 139 | + expect(result?.message).toBe(`update ${updateLevel} for config file 'config.c' has a new version available for merge.`); |
| 140 | + } |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('run-generator action', () => { |
| 145 | + it('encodes generator and context in the command URI arguments', () => { |
| 146 | + const { command, args } = decodeCodeTarget(resolver, makeContext({ |
| 147 | + message: "cgen file was not found, run generator 'CubeMX' for context 'App.Debug+STM32'", |
| 148 | + })); |
| 149 | + |
| 150 | + expect(command).toBe(`command:${RUN_GENERATOR_COMMAND_ID}`); |
| 151 | + expect(args).toEqual([{ generator: 'CubeMX', context: 'App.Debug+STM32' }]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('returns no message field, only a code', () => { |
| 155 | + const result = resolver.resolve(makeContext({ |
| 156 | + message: "cgen file was not found, run generator 'CubeMX' for context 'App.Debug+STM32'", |
| 157 | + })); |
| 158 | + |
| 159 | + expect(result?.message).toBeUndefined(); |
| 160 | + expect(result?.code).toBeDefined(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('is suppressed when hasLocation is true', () => { |
| 164 | + const result = resolver.resolve(makeContext({ |
| 165 | + message: "cgen file was not found, run generator 'CubeMX' for context 'App.Debug+STM32'", |
| 166 | + hasLocation: true, |
| 167 | + })); |
| 168 | + |
| 169 | + expect(result).toBeUndefined(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('matches alternate wording with trailing context description', () => { |
| 173 | + const result = resolver.resolve(makeContext({ |
| 174 | + message: "run generator 'CubeMX2' for context 'CubeMX2.Debug+STM32C531CBT6' to generate missing cgen artifacts", |
| 175 | + })); |
| 176 | + |
| 177 | + expect((result?.code as { value: string } | undefined)?.value).toBe('Run Generator'); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('manage-components action', () => { |
| 182 | + it('is returned for dependency validation context messages', () => { |
| 183 | + const { code } = decodeCodeTarget(resolver, makeContext({ |
| 184 | + message: "dependency validation for context 'App.Debug+STM32' failed:", |
| 185 | + })); |
| 186 | + |
| 187 | + expect(code?.value).toBe('Manage Components'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('encodes the context value in the command URI arguments', () => { |
| 191 | + const { command, args } = decodeCodeTarget(resolver, makeContext({ |
| 192 | + message: "dependency validation for context 'App.Debug+STM32' failed:", |
| 193 | + })); |
| 194 | + |
| 195 | + expect(command).toBe(`command:${MANAGE_COMPONENTS_PACKS_COMMAND_ID}`); |
| 196 | + expect(args).toEqual([{ type: 'context', value: 'App.Debug+STM32' }]); |
| 197 | + }); |
| 198 | + |
| 199 | + it('is suppressed when hasLocation is true', () => { |
| 200 | + const result = resolver.resolve(makeContext({ |
| 201 | + message: "dependency validation for context 'App.Debug+STM32' failed:", |
| 202 | + hasLocation: true, |
| 203 | + })); |
| 204 | + |
| 205 | + expect(result).toBeUndefined(); |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | + describe('find-in-files action', () => { |
| 210 | + it('extracts query from a quoted string in the message', () => { |
| 211 | + const { code } = decodeCodeTarget(resolver, makeContext({ |
| 212 | + message: "component 'Arm::Device@1.0.0' is missing", |
| 213 | + })); |
| 214 | + |
| 215 | + expect(code?.value).toBe('Find in Files'); |
| 216 | + }); |
| 217 | + |
| 218 | + it('extracts query from a slash-delimited file path in the message', () => { |
| 219 | + const { args } = decodeCodeTarget(resolver, makeContext({ |
| 220 | + message: 'file not found: /components/board/startup.c', |
| 221 | + })); |
| 222 | + |
| 223 | + expect(args.query).toBe('startup.c'); |
| 224 | + }); |
| 225 | + |
| 226 | + it('limits search to yml and yaml source files', () => { |
| 227 | + const result = resolver.resolve(makeContext({ |
| 228 | + message: "component 'Arm::Device' is missing", |
| 229 | + })); |
| 230 | + |
| 231 | + const code = result?.code as { target: vscode.Uri } | undefined; |
| 232 | + const raw = code?.target.toString() ?? ''; |
| 233 | + const encoded = raw.split('?')[1] ?? ''; |
| 234 | + const findArgs = JSON.parse(decodeURIComponent(encoded)); |
| 235 | + |
| 236 | + expect(findArgs.filesToInclude).toBe('*.yml,*.yaml'); |
| 237 | + }); |
| 238 | + |
| 239 | + it('excludes generated yml artifacts from the search', () => { |
| 240 | + const result = resolver.resolve(makeContext({ |
| 241 | + message: "component 'Arm::Device' is missing", |
| 242 | + })); |
| 243 | + |
| 244 | + const code = result?.code as { target: vscode.Uri } | undefined; |
| 245 | + const raw = code?.target.toString() ?? ''; |
| 246 | + const encoded = raw.split('?')[1] ?? ''; |
| 247 | + const findArgs = JSON.parse(decodeURIComponent(encoded)); |
| 248 | + |
| 249 | + expect(findArgs.filesToExclude).toBe('*.cbuild-idx.yml,*.cbuild.yml,*.cbuild-run.yml'); |
| 250 | + }); |
| 251 | + |
| 252 | + it('triggers search automatically with focusResults enabled', () => { |
| 253 | + const result = resolver.resolve(makeContext({ |
| 254 | + message: "component 'Arm::Device' is missing", |
| 255 | + })); |
| 256 | + |
| 257 | + const code = result?.code as { target: vscode.Uri } | undefined; |
| 258 | + const raw = code?.target.toString() ?? ''; |
| 259 | + const encoded = raw.split('?')[1] ?? ''; |
| 260 | + const findArgs = JSON.parse(decodeURIComponent(encoded)); |
| 261 | + |
| 262 | + expect(findArgs.triggerSearch).toBe(true); |
| 263 | + expect(findArgs.focusResults).toBe(true); |
| 264 | + }); |
| 265 | + |
| 266 | + it('is suppressed when hasLocation is true', () => { |
| 267 | + const result = resolver.resolve(makeContext({ |
| 268 | + message: "component 'Arm::Device' is missing", |
| 269 | + hasLocation: true, |
| 270 | + })); |
| 271 | + |
| 272 | + expect(result).toBeUndefined(); |
| 273 | + }); |
| 274 | + |
| 275 | + it('returns no message field, only a code', () => { |
| 276 | + const result = resolver.resolve(makeContext({ |
| 277 | + message: "component 'Arm::Device' is missing", |
| 278 | + })); |
| 279 | + |
| 280 | + expect(result?.message).toBeUndefined(); |
| 281 | + expect(result?.code).toBeDefined(); |
| 282 | + }); |
| 283 | + }); |
| 284 | +}); |
0 commit comments