/**
 * workflow-template-compiler.test.ts — Tests for `/sf start` template compilation.
 *
 * Verifies that registry templates compile into valid graph-runtime workflow
 * definitions without requiring users to author YAML files.
 */

import assert from "node:assert/strict";
import { test } from 'vitest';

import { validateDefinition } from "../definition-loader.ts";
import { compileTemplateRun } from "../workflow-template-compiler.ts";
import { loadWorkflowTemplate, resolveByName } from "../workflow-templates.ts";

test("compileTemplateRun turns product-plan into a valid linear workflow definition", () => {
	const match = resolveByName("product-plan");
	assert.ok(match, "product-plan should resolve");
	const workflowContent = loadWorkflowTemplate("product-plan");
	assert.ok(workflowContent, "product-plan template should load");

	const definition = compileTemplateRun({
		templateId: match.id,
		template: match.template,
		workflowContent,
		description: "plan the product we need to develop",
		issueRef: "ISSUE-123",
		artifactDir: ".sf/workflows/product-plan/260501-1-plan",
		branch: "sf/product-plan/plan",
		date: "2026-05-01",
		mode: "guided",
	});

	const validation = validateDefinition(definition);
	assert.equal(
		validation.valid,
		true,
		`definition should validate: ${validation.errors.join("; ")}`,
	);
	assert.equal(definition.name, "product-plan");
	assert.deepEqual(
		definition.steps.map((step) => step.id),
		["model", "scope", "slice", "ready"],
	);
	assert.deepEqual(definition.steps[0].requires, []);
	assert.deepEqual(definition.steps[1].requires, ["model"]);
	assert.ok(
		definition.steps[0].prompt.includes("Product Plan Workflow"),
		"step prompt should include template content",
	);
	assert.ok(
		definition.steps[0].prompt.includes("plan the product we need to develop"),
		"step prompt should include user description",
	);
	assert.ok(
		definition.steps[0].prompt.includes("Current phase: model"),
		"step prompt should name current phase",
	);
	assert.ok(
		definition.steps[0].prompt.includes("This is a guided /sf start run"),
		"guided prompt should permit clarifying questions",
	);
	assert.equal(
		definition.steps.find((step) => step.id === "scope")?.verify?.policy,
		"human-review",
		"scope phase should include a guided review gate",
	);
	assert.equal(
		definition.steps.find((step) => step.id === "ready")?.verify?.policy,
		"human-review",
		"ready phase should include a guided review gate",
	);
});
