fix: unroll edge cases

This commit is contained in:
Nate 2025-11-26 11:04:00 -08:00
parent fa96e30a3a
commit d045072c08
2 changed files with 92 additions and 2 deletions

View file

@ -1,5 +1,7 @@
import { PackageIdentifier } from "../interfaces/slugs.js";
import {
fillTemplateVariables,
getTemplateVariables,
parseMarkdownRuleOrAssistantUnrolled,
replaceInputsWithSecrets,
} from "./unroll.js";
@ -274,3 +276,73 @@ data:
expect(result).not.toContain("inputs.dbPassword");
});
});
describe("getTemplateVariables edge cases", () => {
it("handles undefined input gracefully", () => {
const result = getTemplateVariables(undefined as any);
expect(result).toEqual([]);
});
it("handles null input gracefully", () => {
const result = getTemplateVariables(null as any);
expect(result).toEqual([]);
});
it("handles non-string input gracefully", () => {
const result = getTemplateVariables(123 as any);
expect(result).toEqual([]);
});
it("handles empty string correctly", () => {
const result = getTemplateVariables("");
expect(result).toEqual([]);
});
it("extracts template variables from valid input", () => {
const result = getTemplateVariables("\${{ secrets.apiKey }}");
expect(result).toEqual(["secrets.apiKey"]);
});
it("extracts multiple template variables", () => {
const result = getTemplateVariables(
"\${{ secrets.key1 }} and \${{ inputs.key2 }}",
);
expect(result).toContain("secrets.key1");
expect(result).toContain("inputs.key2");
expect(result.length).toBe(2);
});
});
describe("fillTemplateVariables edge cases", () => {
it("handles undefined input gracefully", () => {
const result = fillTemplateVariables(undefined as any, {});
expect(result).toBe("");
});
it("handles null input gracefully", () => {
const result = fillTemplateVariables(null as any, {});
expect(result).toBe("");
});
it("handles non-string input gracefully", () => {
const result = fillTemplateVariables(123 as any, {});
expect(result).toBe("");
});
it("handles empty string correctly", () => {
const result = fillTemplateVariables("", {});
expect(result).toBe("");
});
it("fills template variables with valid input", () => {
const result = fillTemplateVariables("\${{ secrets.apiKey }}", {
"secrets.apiKey": "my-secret-key",
});
expect(result).toBe("my-secret-key");
});
it("leaves unfilled variables unchanged", () => {
const result = fillTemplateVariables("\${{ secrets.apiKey }}", {});
expect(result).toBe("\${{ secrets.apiKey }}");
});
});

View file

@ -86,6 +86,11 @@ export function parseBlock(configYaml: string): Block {
export const TEMPLATE_VAR_REGEX = /\${{[\s]*([^}\s]+)[\s]*}}/g;
export function getTemplateVariables(templatedYaml: string): string[] {
// Defensive guard against undefined/null/non-string values
if (!templatedYaml || typeof templatedYaml !== "string") {
return [];
}
const variables = new Set<string>();
const matches = templatedYaml.matchAll(TEMPLATE_VAR_REGEX);
for (const match of matches) {
@ -98,6 +103,11 @@ export function fillTemplateVariables(
templatedYaml: string,
data: { [key: string]: string },
): string {
// Defensive guard against undefined/null/non-string values
if (!templatedYaml || typeof templatedYaml !== "string") {
return "";
}
return templatedYaml.replace(TEMPLATE_VAR_REGEX, (match, variableName) => {
// Inject data
if (variableName in data) {
@ -701,7 +711,7 @@ function injectLocalSourceFile(
export async function resolveBlock(
id: PackageIdentifier,
inputs: Record<string, string> | undefined,
inputs: Record<string, string | undefined> | undefined,
registry: Registry,
): Promise<AssistantUnrolled> {
// Retrieve block raw yaml
@ -777,11 +787,19 @@ function parseYamlOrMarkdownRule<T>(
}
function inputsToFQSNs(
inputs: Record<string, string>,
inputs: Record<string, string | undefined>,
blockIdentifier: PackageIdentifier,
): Record<string, string> {
const renderedInputs: Record<string, string> = {};
for (const [key, value] of Object.entries(inputs)) {
// Skip undefined, null, or non-string values
if (value === undefined || value === null || typeof value !== "string") {
console.warn(
`Skipping input "${key}" with invalid value type: ${typeof value}. Expected string.`,
);
continue;
}
renderedInputs[key] = renderTemplateData(value, {
secrets: extractFQSNMap(value, [blockIdentifier]),
});