Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions apps/cli/src/config-loader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it, expect, afterAll } from "vitest";
import { writeFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";

import { resolveScenarioAndPersona } from "./config-loader.js";

const dir = mkdtempSync(join(tmpdir(), "scope-config-loader-"));
afterAll(() => rmSync(dir, { recursive: true, force: true }));

function write(name: string, content: string): string {
const path = join(dir, name);
writeFileSync(path, content, "utf-8");
return path;
}

const validScenario = write(
"scenario.yaml",
'task: "summarize the file"\ncriteria:\n - correctness\n'
);

describe("resolveScenarioAndPersona config validation", () => {
it("reports a clear error for an empty scenario file (not a TypeError)", () => {
const scenario = write("empty-scenario.yaml", "");
expect(() => resolveScenarioAndPersona(scenario)).toThrowError(
/must have a "task" string field/
);
});

it("reports a clear error for a comment-only scenario file", () => {
const scenario = write("comment-scenario.yaml", "# nothing here\n");
expect(() => resolveScenarioAndPersona(scenario)).toThrowError(
/must have a "task" string field/
);
});

it("reports a clear error for a comment-only persona file (not a TypeError)", () => {
const persona = write("comment-persona.yaml", "# just a comment\n");
expect(() => resolveScenarioAndPersona(validScenario, persona)).toThrowError(
/is empty or not a YAML mapping/
);
});

it("still loads a valid scenario without a persona", () => {
const result = resolveScenarioAndPersona(validScenario);
expect(result.task).toBe("summarize the file");
expect(result.criteria).toEqual(["correctness"]);
});
});
14 changes: 14 additions & 0 deletions apps/cli/src/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function loadPersona(filePath: string): Persona {
const raw = readFileSync(filePath, "utf-8");
const data = parseYaml(raw);

if (!data || typeof data !== "object") {
throw new Error(`Persona file ${filePath} is empty or not a YAML mapping`);
}

const validPersonality: Personality[] = ["demanding", "friendly"];
const validExperience: Experience[] = ["junior", "senior"];
const validVerbosity: Verbosity[] = ["brief", "moderate"];
Expand Down Expand Up @@ -75,6 +79,10 @@ function loadScenario(filePath: string): Scenario {
const raw = readFileSync(filePath, "utf-8");
const data = parseYaml(raw);

if (!data || typeof data !== "object") {
throw new Error(`Scenario file ${filePath} must have a "task" string field`);
}

if (!data.task || typeof data.task !== "string") {
throw new Error(`Scenario file ${filePath} must have a "task" string field`);
}
Expand Down Expand Up @@ -108,6 +116,12 @@ function loadTraits(filePath: string): TraitDescriptions {
const raw = readFileSync(filePath, "utf-8");
const data = parseYaml(raw);

if (!data || typeof data !== "object") {
throw new Error(
`Traits file ${filePath} must have personality, experience, verbosity, and type sections`
);
}

if (!data.personality || !data.experience || !data.verbosity || !data.type) {
throw new Error(
`Traits file ${filePath} must have personality, experience, verbosity, and type sections`
Expand Down