Changesets · Branches · Tags · Compare
| Changeset | ad83f3cc53014d5a0d0dffc459aab24775ba47e806dacf7fd37685577131a90a |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-18 17:12 +02:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-18 17:12 +02:00 |
| Parents | beec9aab9cdf, 36062bf22f47 |
2 files changed
added packages/config-yaml/src/load/unroll.test.ts +91 −0
| @@ -0,0 +1,91 @@ | ||
| 1 | +import { PackageIdentifier } from "../interfaces/slugs.js"; | |
| 2 | +import { parseMarkdownRuleOrAssistantUnrolled } from "./unroll.js"; | |
| 3 | + | |
| 4 | +describe("parseMarkdownRuleOrAssistantUnrolled tests", () => { | |
| 5 | + it("parses valid YAML content as AssistantUnrolled", () => { | |
| 6 | + const mockId: PackageIdentifier = { | |
| 7 | + uriType: "file", | |
| 8 | + filePath: "./foo/bar.yaml", | |
| 9 | + }; | |
| 10 | + const yamlContent = ` | |
| 11 | +name: Test Assistant | |
| 12 | +version: 1.0.0 | |
| 13 | + | |
| 14 | +models: | |
| 15 | + - name: model | |
| 16 | + model: modelname | |
| 17 | + provider: ollama | |
| 18 | +`; | |
| 19 | + const result = parseMarkdownRuleOrAssistantUnrolled(yamlContent, mockId); | |
| 20 | + expect(result).toHaveProperty("name", "Test Assistant"); | |
| 21 | + expect(result).toHaveProperty("version", "1.0.0"); | |
| 22 | + expect(result.models?.length).toBe(1); | |
| 23 | + expect(result.models?.[0]).toHaveProperty("name", "model"); | |
| 24 | + }); | |
| 25 | + | |
| 26 | + it("parses markdown rule content as AssistantUnrolled", () => { | |
| 27 | + const mockId: PackageIdentifier = { | |
| 28 | + uriType: "file", | |
| 29 | + filePath: "./foo/bar.md", | |
| 30 | + }; | |
| 31 | + const markdownContent = ` | |
| 32 | +--- | |
| 33 | +name: rulename | |
| 34 | +description: my rule description | |
| 35 | +pattern: "**" | |
| 36 | +--- | |
| 37 | +This is the rule | |
| 38 | +`; | |
| 39 | + const result = parseMarkdownRuleOrAssistantUnrolled( | |
| 40 | + markdownContent, | |
| 41 | + mockId, | |
| 42 | + ); | |
| 43 | + expect(result).toHaveProperty("name"); | |
| 44 | + expect(result).toHaveProperty("version"); | |
| 45 | + expect(Array.isArray(result.rules)).toBe(true); | |
| 46 | + expect(result.rules).toBeDefined(); | |
| 47 | + expect(result.rules!.length).toBe(1); | |
| 48 | + const rule = result.rules![0]; | |
| 49 | + expect(rule).toHaveProperty("name", "rulename"); | |
| 50 | + expect(rule).toHaveProperty("description", "my rule description"); | |
| 51 | + expect(rule).toHaveProperty("globs", "./foo/**/*"); | |
| 52 | + expect(rule).toHaveProperty("rule", "This is the rule"); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + it("throws error for invalid content in yaml file", () => { | |
| 56 | + const mockId: PackageIdentifier = { | |
| 57 | + uriType: "file", | |
| 58 | + filePath: "./foo/bar.yaml", | |
| 59 | + }; | |
| 60 | + const invalidContent = ` | |
| 61 | +name: Test Assistant | |
| 62 | +version: 1.0.0 | |
| 63 | + | |
| 64 | +model: # should be models | |
| 65 | + - name: model | |
| 66 | + model: modelname | |
| 67 | + provider: ollama | |
| 68 | +`; | |
| 69 | + expect(() => | |
| 70 | + parseMarkdownRuleOrAssistantUnrolled(invalidContent, mockId), | |
| 71 | + ).toThrow(); | |
| 72 | + }); | |
| 73 | + | |
| 74 | + it("Every non-YAML file is a rule", () => { | |
| 75 | + const mockId: PackageIdentifier = { | |
| 76 | + uriType: "file", | |
| 77 | + filePath: "./foo/bar", | |
| 78 | + }; | |
| 79 | + const dubiousContent = `This is not \nproper #YAML#`; | |
| 80 | + const result = parseMarkdownRuleOrAssistantUnrolled(dubiousContent, mockId); | |
| 81 | + expect(result).toHaveProperty("name"); | |
| 82 | + expect(result).toHaveProperty("version"); | |
| 83 | + expect(Array.isArray(result.rules)).toBe(true); | |
| 84 | + expect(result.rules).toBeDefined(); | |
| 85 | + expect(result.rules!.length).toBe(1); | |
| 86 | + const rule = result.rules![0]; | |
| 87 | + expect(rule).toHaveProperty("name", "foo/bar"); | |
| 88 | + expect(rule).toHaveProperty("globs", "./foo/**/*"); | |
| 89 | + expect(rule).toHaveProperty("rule", dubiousContent); | |
| 90 | + }); | |
| 91 | +}); |
modified packages/config-yaml/src/load/unroll.ts +25 −52
| @@ -286,9 +286,7 @@ | ||
| 286 | 286 | options.platformClient, |
| 287 | 287 | options.alwaysUseProxy, |
| 288 | 288 | ); |
| 289 | - const renderedYaml = renderTemplateData(templatedYaml, { | |
| 290 | - secrets, | |
| 291 | - }); | |
| 289 | + const renderedYaml = renderTemplateData(templatedYaml, { secrets }); | |
| 292 | 290 | |
| 293 | 291 | // Parse again and replace models with proxy versions where secrets weren't rendered |
| 294 | 292 | const finalConfig = useProxyForUnrenderedSecrets( |
| @@ -298,11 +296,7 @@ | ||
| 298 | 296 | options.onPremProxyUrl, |
| 299 | 297 | ); |
| 300 | 298 | |
| 301 | - return { | |
| 302 | - config: finalConfig, | |
| 303 | - errors, | |
| 304 | - configLoadInterrupted, | |
| 305 | - }; | |
| 299 | + return { config: finalConfig, errors, configLoadInterrupted }; | |
| 306 | 300 | } |
| 307 | 301 | |
| 308 | 302 | export async function unrollBlocks( |
| @@ -366,10 +360,7 @@ | ||
| 366 | 360 | return { |
| 367 | 361 | index, |
| 368 | 362 | block: null, |
| 369 | - error: { | |
| 370 | - fatal: false, | |
| 371 | - message: msg, | |
| 372 | - }, | |
| 363 | + error: { fatal: false, message: msg }, | |
| 373 | 364 | }; |
| 374 | 365 | } |
| 375 | 366 | } else { |
| @@ -461,11 +452,7 @@ | ||
| 461 | 452 | registry, |
| 462 | 453 | ); |
| 463 | 454 | |
| 464 | - return { | |
| 465 | - blockType, | |
| 466 | - resolvedBlock, | |
| 467 | - error: null, | |
| 468 | - }; | |
| 455 | + return { blockType, resolvedBlock, error: null }; | |
| 469 | 456 | } catch (err) { |
| 470 | 457 | let msg = ""; |
| 471 | 458 | if (injectBlock.uriType === "file") { |
| @@ -481,10 +468,7 @@ | ||
| 481 | 468 | return { |
| 482 | 469 | blockType: null, |
| 483 | 470 | resolvedBlock: null, |
| 484 | - error: { | |
| 485 | - fatal: false, | |
| 486 | - message: msg, | |
| 487 | - }, | |
| 471 | + error: { fatal: false, message: msg }, | |
| 488 | 472 | }; |
| 489 | 473 | } |
| 490 | 474 | }); |
| @@ -581,57 +565,46 @@ | ||
| 581 | 565 | return parseMarkdownRuleOrAssistantUnrolled(templatedYaml, id); |
| 582 | 566 | } |
| 583 | 567 | |
| 584 | -function parseMarkdownRuleOrAssistantUnrolled( | |
| 568 | +export function parseMarkdownRuleOrAssistantUnrolled( | |
| 585 | 569 | content: string, |
| 586 | 570 | id: PackageIdentifier, |
| 587 | 571 | ): AssistantUnrolled { |
| 588 | - // Try to parse as YAML first, then as markdown rule if that fails | |
| 589 | - let parsedYaml: AssistantUnrolled; | |
| 590 | - try { | |
| 591 | - parsedYaml = parseBlock(content); | |
| 592 | - } catch (yamlError) { | |
| 593 | - // If YAML parsing fails, try parsing as markdown rule | |
| 594 | - try { | |
| 595 | - const rule = markdownToRule(content, id); | |
| 596 | - // Convert the rule object to the expected format | |
| 597 | - parsedYaml = { | |
| 598 | - name: rule.name, | |
| 599 | - version: "1.0.0", | |
| 600 | - rules: [rule], | |
| 601 | - }; | |
| 602 | - } catch (markdownError) { | |
| 603 | - // If both fail, throw the original YAML error | |
| 604 | - throw yamlError; | |
| 605 | - } | |
| 606 | - } | |
| 607 | - | |
| 608 | - return parsedYaml; | |
| 572 | + return parseYamlOrMarkdownRule<AssistantUnrolled>(content, id, parseBlock); | |
| 609 | 573 | } |
| 610 | 574 | |
| 611 | 575 | function parseMarkdownRuleOrConfigYaml( |
| 612 | 576 | content: string, |
| 613 | 577 | id: PackageIdentifier, |
| 614 | 578 | ): ConfigYaml { |
| 615 | - // Try to parse as YAML first, then as markdown rule if that fails | |
| 616 | - let parsedYaml: ConfigYaml; | |
| 579 | + return parseYamlOrMarkdownRule<ConfigYaml>(content, id, parseConfigYaml); | |
| 580 | +} | |
| 581 | + | |
| 582 | +function parseYamlOrMarkdownRule<T>( | |
| 583 | + content: string, | |
| 584 | + id: PackageIdentifier, | |
| 585 | + parseYamlFn: (content: string) => T, | |
| 586 | +): T { | |
| 587 | + let parsedYaml: T; | |
| 617 | 588 | try { |
| 618 | - parsedYaml = parseConfigYaml(content); | |
| 589 | + // Try to parse as YAML first, then as markdown rule if that fails | |
| 590 | + parsedYaml = parseYamlFn(content); | |
| 619 | 591 | } catch (yamlError) { |
| 592 | + if ( | |
| 593 | + id.uriType === "file" && | |
| 594 | + [".yaml", ".yml"].some((ext) => id.filePath.endsWith(ext)) | |
| 595 | + ) { | |
| 596 | + throw yamlError; | |
| 597 | + } | |
| 620 | 598 | // If YAML parsing fails, try parsing as markdown rule |
| 621 | 599 | try { |
| 622 | 600 | const rule = markdownToRule(content, id); |
| 623 | 601 | // Convert the rule object to the expected format |
| 624 | - parsedYaml = { | |
| 625 | - name: rule.name, | |
| 626 | - version: "1.0.0", | |
| 627 | - rules: [rule], | |
| 628 | - }; | |
| 602 | + parsedYaml = { name: rule.name, version: "1.0.0", rules: [rule] } as T; | |
| 629 | 603 | } catch (markdownError) { |
| 630 | 604 | // If both fail, throw the original YAML error |
| 631 | 605 | throw yamlError; |
| 632 | 606 | } |
| 633 | 607 | } |
| 634 | - | |
| 635 | 608 | return parsedYaml; |
| 636 | 609 | } |
| 637 | 610 |