|
1 | | -import { extractJsonObject, translateOnboardPayload } from './agentic-onboard'; |
| 1 | +import { |
| 2 | + extractJsonPayloads, |
| 3 | + translateOnboardPayload, |
| 4 | +} from './agentic-onboard'; |
2 | 5 |
|
3 | 6 | describe('translateOnboardPayload', () => { |
4 | 7 | it('returns null for empty or whitespace lines', () => { |
@@ -232,47 +235,157 @@ describe('translateOnboardPayload', () => { |
232 | 235 | }); |
233 | 236 | }); |
234 | 237 |
|
235 | | -describe('extractJsonObject', () => { |
236 | | - it('returns null when there is no JSON object', () => { |
237 | | - expect(extractJsonObject('')).toBeNull(); |
238 | | - expect(extractJsonObject('just human text, no braces')).toBeNull(); |
| 238 | +describe('extractJsonPayloads', () => { |
| 239 | + it('returns [] for empty or whitespace-only input', () => { |
| 240 | + expect(extractJsonPayloads('')).toEqual([]); |
| 241 | + expect(extractJsonPayloads(' \n\n ')).toEqual([]); |
239 | 242 | }); |
240 | 243 |
|
241 | | - it('extracts a flat JSON object from a clean buffer', () => { |
242 | | - expect(extractJsonObject('{"a":1}')).toBe('{"a":1}'); |
| 244 | + it('returns [] when there is no JSON', () => { |
| 245 | + expect(extractJsonPayloads('just human text, no braces')).toEqual([]); |
| 246 | + expect( |
| 247 | + extractJsonPayloads( |
| 248 | + ' NX Updating nx.json\n\nNo JSON in this output anywhere.' |
| 249 | + ) |
| 250 | + ).toEqual([]); |
243 | 251 | }); |
244 | 252 |
|
245 | | - it('extracts the JSON blob when preceded by human-readable text', () => { |
246 | | - const buf = ` |
247 | | - NX Updating nx.json with Nx Cloud ID |
| 253 | + // NDJSON case (target shape after CLOUD-4496). |
| 254 | + it('parses a single NDJSON line', () => { |
| 255 | + expect(extractJsonPayloads('{"stage":"configuring"}')).toEqual([ |
| 256 | + { stage: 'configuring' }, |
| 257 | + ]); |
| 258 | + }); |
| 259 | + |
| 260 | + it('parses multiple NDJSON lines in order', () => { |
| 261 | + const buf = [ |
| 262 | + '{"stage":"configuring","message":"Connecting..."}', |
| 263 | + '{"success":true,"workspace":{"id":"w","nxCloudId":"abc"},"configWritten":true}', |
| 264 | + ].join('\n'); |
| 265 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 266 | + { stage: 'configuring', message: 'Connecting...' }, |
| 267 | + { |
| 268 | + success: true, |
| 269 | + workspace: { id: 'w', nxCloudId: 'abc' }, |
| 270 | + configWritten: true, |
| 271 | + }, |
| 272 | + ]); |
| 273 | + }); |
248 | 274 |
|
249 | | -Your nx.json has been updated to use an Nx Cloud ID for authentication. |
| 275 | + it('parses NDJSON with non-JSON noise interleaved', () => { |
| 276 | + const buf = [ |
| 277 | + ' NX Some bin output', |
| 278 | + '{"stage":"configuring"}', |
| 279 | + 'plain text leak', |
| 280 | + '{"success":true,"workspace":{"nxCloudId":"abc"}}', |
| 281 | + '', |
| 282 | + ].join('\n'); |
| 283 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 284 | + { stage: 'configuring' }, |
| 285 | + { success: true, workspace: { nxCloudId: 'abc' } }, |
| 286 | + ]); |
| 287 | + }); |
250 | 288 |
|
251 | | -{ |
| 289 | + // Multi-line pretty-printed case (current bin via CLOUD-4496 workaround). |
| 290 | + it('parses a single multi-line pretty-printed JSON object', () => { |
| 291 | + const buf = `{ |
252 | 292 | "success": true, |
253 | 293 | "workspace": { |
254 | 294 | "id": "abc", |
255 | 295 | "nxCloudId": "abc123" |
256 | 296 | }, |
257 | 297 | "configWritten": true |
258 | 298 | }`; |
259 | | - const blob = extractJsonObject(buf); |
260 | | - expect(JSON.parse(blob!)).toEqual({ |
261 | | - success: true, |
262 | | - workspace: { id: 'abc', nxCloudId: 'abc123' }, |
263 | | - configWritten: true, |
264 | | - }); |
| 299 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 300 | + { |
| 301 | + success: true, |
| 302 | + workspace: { id: 'abc', nxCloudId: 'abc123' }, |
| 303 | + configWritten: true, |
| 304 | + }, |
| 305 | + ]); |
265 | 306 | }); |
266 | 307 |
|
267 | | - it('ignores braces inside JSON string values', () => { |
268 | | - expect(extractJsonObject('{"msg":"hello {world}","ok":true}')).toBe( |
269 | | - '{"msg":"hello {world}","ok":true}' |
270 | | - ); |
| 308 | + it('parses multi-line JSON preceded by human-readable noise', () => { |
| 309 | + const buf = ` |
| 310 | + NX Updating nx.json with Nx Cloud ID |
| 311 | +
|
| 312 | +Your nx.json has been updated. |
| 313 | +
|
| 314 | +{ |
| 315 | + "success": true, |
| 316 | + "workspace": { "nxCloudId": "abc" }, |
| 317 | + "configWritten": true |
| 318 | +}`; |
| 319 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 320 | + { |
| 321 | + success: true, |
| 322 | + workspace: { nxCloudId: 'abc' }, |
| 323 | + configWritten: true, |
| 324 | + }, |
| 325 | + ]); |
271 | 326 | }); |
272 | 327 |
|
273 | | - it('handles escaped quotes inside strings without losing brace tracking', () => { |
274 | | - expect(extractJsonObject('{"a":"a \\"quoted\\" }","b":1}')).toBe( |
275 | | - '{"a":"a \\"quoted\\" }","b":1}' |
276 | | - ); |
| 328 | + it('parses multi-line JSON followed by trailing noise', () => { |
| 329 | + const buf = `{ |
| 330 | + "success": true, |
| 331 | + "workspace": { "nxCloudId": "abc" } |
| 332 | +} |
| 333 | +NX Some trailing message`; |
| 334 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 335 | + { success: true, workspace: { nxCloudId: 'abc' } }, |
| 336 | + ]); |
| 337 | + }); |
| 338 | + |
| 339 | + // Mixed shapes — multi-line followed by NDJSON, etc. |
| 340 | + it('parses a mix of multi-line JSON and NDJSON in the same buffer', () => { |
| 341 | + const buf = `{"stage":"configuring"} |
| 342 | +{ |
| 343 | + "success": true, |
| 344 | + "workspace": { "nxCloudId": "abc" } |
| 345 | +} |
| 346 | +{"trailing":"event"}`; |
| 347 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 348 | + { stage: 'configuring' }, |
| 349 | + { success: true, workspace: { nxCloudId: 'abc' } }, |
| 350 | + { trailing: 'event' }, |
| 351 | + ]); |
| 352 | + }); |
| 353 | + |
| 354 | + // Robustness against JSON content that itself contains braces. |
| 355 | + it('treats indented inner braces as part of the multi-line object', () => { |
| 356 | + // The inner `{`/`}` are indented (column > 0), so they don't terminate. |
| 357 | + const buf = `{ |
| 358 | + "workspace": { |
| 359 | + "nested": { "deep": true } |
| 360 | + } |
| 361 | +}`; |
| 362 | + expect(extractJsonPayloads(buf)).toEqual([ |
| 363 | + { workspace: { nested: { deep: true } } }, |
| 364 | + ]); |
| 365 | + }); |
| 366 | + |
| 367 | + it('handles braces inside JSON string values (single-line)', () => { |
| 368 | + expect(extractJsonPayloads('{"msg":"hello {world}","ok":true}')).toEqual([ |
| 369 | + { msg: 'hello {world}', ok: true }, |
| 370 | + ]); |
| 371 | + }); |
| 372 | + |
| 373 | + // Failure modes — should not throw, just return []. |
| 374 | + it('skips truncated multi-line JSON (no closing })', () => { |
| 375 | + const buf = `{ |
| 376 | + "success": true, |
| 377 | + "workspace": { "nxCloudId": "abc" }`; |
| 378 | + expect(extractJsonPayloads(buf)).toEqual([]); |
| 379 | + }); |
| 380 | + |
| 381 | + it('skips malformed JSON without crashing', () => { |
| 382 | + expect(extractJsonPayloads('{not valid json}')).toEqual([]); |
| 383 | + expect(extractJsonPayloads('{\n "success": true,\n bad,\n}')).toEqual([]); |
| 384 | + }); |
| 385 | + |
| 386 | + it('rejects non-object top-level values (arrays, primitives)', () => { |
| 387 | + // Bin only emits objects; arrays/strings would be a contract violation. |
| 388 | + expect(extractJsonPayloads('[1,2,3]')).toEqual([]); |
| 389 | + expect(extractJsonPayloads('"just a string"')).toEqual([]); |
277 | 390 | }); |
278 | 391 | }); |
0 commit comments