/**
 * Tests for the goal settings system (.pi/goal-settings.json + env var overrides).
 */
import * as fs from "node:fs";
import * as path from "node:path";
import assert from "node:assert/strict";
import test from "node:test";

import {
	normalizeTaskItem,
	normalizeTaskList,
	type GoalRecord,
} from "../extensions/goal-record.ts";
import { parseSettingsLayer, formatGoalKeybinding } from "../extensions/settings/schema.ts";
import { goalSettingsPath, loadGoalSettingsFileConfig } from "../extensions/settings/files.ts";
import { loadGoalSettings, effectiveSettingsReport } from "../extensions/settings/resolve.ts";
import { writeProjectSettings, parseSettings } from "./support/settings.ts";

// ── parseSettings ───────────────────────────────────────────────────

test("parseSettings: null/undefined returns empty defaults", () => {
	assert.deepEqual(parseSettings(null), {});
	assert.deepEqual(parseSettings(undefined as unknown), {});
	assert.deepEqual(parseSettings(""), {});
	assert.deepEqual(parseSettings(42), {});
	assert.deepEqual(parseSettings([]), {});
});

test("parseSettings: empty object returns empty defaults", () => {
	assert.deepEqual(parseSettings({}), {});
});

test("parseSettings: both flags false returns false defaults", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: false });
	assert.equal(result.disableTasks, false);
	assert.equal(result.disableContracts, false);
});

test("parseSettings: both flags true", () => {
	const result = parseSettings({ disableTasks: true, disableContracts: true });
	assert.equal(result.disableTasks, true);
	assert.equal(result.disableContracts, true);
});

test("parseSettings: boolean false stored correctly", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: true });
	assert.equal(result.disableTasks, false);
	assert.equal(result.disableContracts, true);
});

test("parseSettings: string true/false values accepted", () => {
	assert.deepEqual(parseSettings({ disableTasks: "true", disableContracts: "false" }), {
		disableTasks: true,
		disableContracts: false,
	});
});

test("parseSettings: autoSelectSingleGoal accepted as bool or string; explicit false preserved (layering)", () => {
	assert.deepEqual(parseSettings({ autoSelectSingleGoal: true }), { autoSelectSingleGoal: true });
	assert.deepEqual(parseSettings({ autoSelectSingleGoal: "true" }), { autoSelectSingleGoal: true });
	// Issue #27 layering requirement: explicit false must survive parsing so a
	// project can override a global true.
	assert.deepEqual(parseSettings({ autoSelectSingleGoal: false }), { autoSelectSingleGoal: false });
	assert.deepEqual(parseSettings({ autoSelectSingleGoal: "false" }), { autoSelectSingleGoal: false });
});

test("parseSettings: unknown keys rejected", () => {
	assert.throws(
		() => parseSettings({ disableTasks: true, disableContracts: false, foo: "bar" }),
		/Unknown pi-goal-x-settings.json key/,
	);
});

test("formatGoalKeybinding: renders readable labels for named keys", () => {
	assert.equal(formatGoalKeybinding("ctrl+shift+t"), "Ctrl+Shift+T");
	assert.equal(formatGoalKeybinding("ctrl+shift+up"), "Ctrl+Shift+↑");
	assert.equal(formatGoalKeybinding("ctrl+shift+pageUp"), "Ctrl+Shift+PageUp");
	assert.equal(formatGoalKeybinding("ctrl+alt+pageDown"), "Ctrl+Alt+PageDown");
	assert.equal(formatGoalKeybinding("ctrl+home"), "Ctrl+Home");
	assert.equal(formatGoalKeybinding("ctrl+alt+enter"), "Ctrl+Alt+Enter");
});

test("parseSettings: task keybindings use pi-tui key names", () => {
	assert.deepEqual(parseSettings({ keybindings: { dashboard: {
		toggleExpand: "ctrl+shift+t",
		scrollUp: "ctrl+shift+up",
		scrollDown: "ctrl+shift+down",
	} } }), { keybindings: { dashboard: {
		toggleExpand: "ctrl+shift+t",
		scrollUp: "ctrl+shift+up",
		scrollDown: "ctrl+shift+down",
	} } });
	assert.equal(loadGoalSettings("/tmp/does-not-exist", {}).keybindings?.dashboard.toggleExpand, "ctrl+shift+t");
});

test("parseSettings: unknown nested task keybindings become diagnostics, valid siblings still apply", () => {
	const result = parseSettingsLayer({ keybindings: { dashboard: { expand: "ctrl+t", scrollUp: "ctrl+u" } } }, "project", "t.json");
	assert.equal(result.layer.keybindings?.dashboard?.scrollUp, "ctrl+u", "valid sibling key still applies");
	assert.ok(result.diagnostics.some((d) => d.code === "unknown_key" && d.settingPath === "keybindings.dashboard.expand"));
});

test("parseSettings: multiple unknown keys rejected with the full list", () => {
	assert.throws(
		() => parseSettings({ disableTasks: true, foo: "bar", baz: 42 }),
		/foo, baz/,
	);
});

// ── goalSettingsPath ────────────────────────────────────────────────────

test("idleContinuationBackoff uses the existing strict boolean normalization", () => {
	for (const [input, expected] of [[true, true], [false, false], ["true", true], ["false", false]] as const) {
		assert.deepEqual(parseSettings({ idleContinuationBackoff: input }), { idleContinuationBackoff: expected });
	}
	for (const invalid of [1, null, "enabled", {}, []]) {
		const parsed = parseSettingsLayer({ idleContinuationBackoff: invalid }, "project", "inline");
		assert.equal(parsed.layer.idleContinuationBackoff, undefined);
		assert.ok(parsed.diagnostics.some(d => d.code === "invalid_value" && d.settingPath === "idleContinuationBackoff"));
	}
});

test("goalSettingsPath: resolves under .pi/ with new filename", () => {
	const p = goalSettingsPath("/tmp/project");
	assert.ok(p.endsWith(path.join(".pi", "pi-goal-x-settings.json")));
	assert.ok(p.startsWith("/tmp/project"));
});

test("goalSettingsPath: respects PI_GOAL_SETTINGS_FILE env var", () => {
	// Relative path
	const rel = goalSettingsPath("/tmp/project", { PI_GOAL_SETTINGS_FILE: "custom-settings.json" });
	assert.equal(rel, path.join("/tmp/project", "custom-settings.json"));

	// Absolute path
	const abs = goalSettingsPath("/tmp/project", { PI_GOAL_SETTINGS_FILE: "/etc/pi/settings.json" });
	assert.equal(abs, "/etc/pi/settings.json");

	// No env var — defaults to .pi/pi-goal-x-settings.json
	const def = goalSettingsPath("/tmp/project", {});
	assert.ok(def.endsWith(path.join(".pi", "pi-goal-x-settings.json")));
});

// ── loadGoalSettingsFileConfig ──────────────────────────────────────────

function withTempDir(fn: (dir: string) => void): void {
	const dir = tempDir("goal-settings-test-");
	try {
		fn(dir);
	} finally {
		fs.rmSync(dir, { recursive: true, force: true });
	}
}

test("loadGoalSettingsFileConfig: missing file returns empty defaults", () => {
	withTempDir((dir) => {
		const result = loadGoalSettingsFileConfig(dir);
		assert.deepEqual(result, {});
	});
});

test("loadGoalSettingsFileConfig: reads valid file config", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: true, disableContracts: false }), "utf8");
		const result = loadGoalSettingsFileConfig(dir);
		assert.equal(result.disableTasks, true);
		assert.equal(result.disableContracts, false);
	});
});

test("loadGoalSettingsFileConfig: malformed JSON returns empty defaults", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, "not-json", "utf8");
		const result = loadGoalSettingsFileConfig(dir);
		assert.deepEqual(result, {});
	});
});

test("loadGoalSettingsFileConfig: unknown keys diagnosed while known values still load (layered rewrite)", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: true, extra: "bad" }), "utf8");
		// Layered behavior: the valid known key still applies; the unknown key
		// is a visible diagnostic instead of silently erasing the whole file.
		const result = loadGoalSettingsFileConfig(dir);
		assert.equal(result.disableTasks, true);
	});
});

// ── loadGoalSettings (env var overrides) ────────────────────────────────

test("loadGoalSettings: no file, no env vars -> defaults false", () => {
	withTempDir((dir) => {
		const result = loadGoalSettings(dir, {});
		assert.equal(result.disableTasks, false);
		assert.equal(result.disableContracts, false);
		assert.equal(result.autoSelectSingleGoal, false, "autoSelectSingleGoal defaults to false");
	});
});

test("loadGoalSettings: env vars override file config", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		// File says both should true
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: true, disableContracts: true }), "utf8");
		// Env says only disableTasks should be false (overriding file)
		const result = loadGoalSettings(dir, { PI_GOAL_DISABLE_TASKS: "false", PI_GOAL_DISABLE_CONTRACTS: "true" });
		assert.equal(result.disableTasks, false, "env override should win");
		assert.equal(result.disableContracts, true, "file value used when no env override");
	});
});

test("loadGoalSettings: env var true overrides file false", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false }), "utf8");
		const result = loadGoalSettings(dir, { PI_GOAL_DISABLE_TASKS: "true" });
		assert.equal(result.disableTasks, true);
		assert.equal(result.disableContracts, false);
	});
});

test("loadGoalSettings: env var absent falls back to file", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: true, disableContracts: true }), "utf8");
		const result = loadGoalSettings(dir, { SOME_OTHER_VAR: "x" });
		assert.equal(result.disableTasks, true);
		assert.equal(result.disableContracts, true);
	});
});

test("loadGoalSettings: env var non-true values treated as absent", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false }), "utf8");
		const result = loadGoalSettings(dir, { PI_GOAL_DISABLE_TASKS: "1", PI_GOAL_DISABLE_CONTRACTS: "" });
		assert.equal(result.disableTasks, false, "1 is not 'true'");
		assert.equal(result.disableContracts, false, "empty string treated as absent");
	});
});

test("loadGoalSettings: no file, env var true", () => {
	withTempDir((dir) => {
		const result = loadGoalSettings(dir, { PI_GOAL_DISABLE_TASKS: "true", PI_GOAL_DISABLE_CONTRACTS: "true" });
		assert.equal(result.disableTasks, true);
		assert.equal(result.disableContracts, true);
	});
});

test("loadGoalSettings: both flags disabled via file", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: true, disableContracts: true }), "utf8");
		const result = loadGoalSettings(dir, {});
		assert.equal(result.disableTasks, true);
		assert.equal(result.disableContracts, true);
	});
});

// ── objectiveMaxChars (goal length limit setting) ────────────────────────

test("parseSettings: objectiveMaxChars accepted as number or string, 0 allowed", () => {
	assert.deepEqual(parseSettings({ objectiveMaxChars: 0 }), { objectiveMaxChars: 0 });
	assert.deepEqual(parseSettings({ objectiveMaxChars: 5000 }), { objectiveMaxChars: 5000 });
	assert.deepEqual(parseSettings({ objectiveMaxChars: "2500" }), { objectiveMaxChars: 2500 });
	assert.deepEqual(parseSettings({ objectiveMaxChars: -1 }), {}, "negative rejected (invalid settings ignored)");
	assert.deepEqual(parseSettings({ objectiveMaxChars: 1.5 }), {}, "non-integer rejected");
});

test("loadGoalSettings: objectiveMaxChars defaults to no limit and honors the env override", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ objectiveMaxChars: 2000 }), "utf8");
		assert.equal(loadGoalSettings(dir, {}).objectiveMaxChars, 2000, "file config read");
		assert.equal(loadGoalSettings(dir, { PI_GOAL_OBJECTIVE_MAX_CHARS: "8000" }).objectiveMaxChars, 8000, "env var overrides file");
	});
	assert.equal(loadGoalSettings("/tmp/does-not-exist", {}).objectiveMaxChars, 0, "unset resolves to the default 0 (no limit)");
});

// ── networkRecovery (provider-error recovery backoff) ─────────────────

test("parseSettings: networkRecovery accepts valid layers and rejects invalid leaves", () => {
	assert.deepEqual(parseSettings({ networkRecovery: { maxAttempts: 0 } }), { networkRecovery: { maxAttempts: 0 } });
	assert.deepEqual(parseSettings({ networkRecovery: { maxAttempts: 8, maxDelayMs: 30_000 } }), {
		networkRecovery: { maxAttempts: 8, maxDelayMs: 30_000 },
	});
	assert.deepEqual(parseSettings({ networkRecovery: { maxAttempts: -1 } }), {}, "negative attempts rejected");
	assert.deepEqual(parseSettings({ networkRecovery: { maxDelayMs: 500 } }), {}, "plateau below 1s rejected");
	assert.deepEqual(parseSettings({ networkRecovery: "nope" }), {}, "non-object rejected");
});

test("loadGoalSettings: networkRecovery defaults to unbounded and honors file + env layers", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ networkRecovery: { maxAttempts: 3 } }), "utf8");
		assert.deepEqual(loadGoalSettings(dir, {}).networkRecovery, { maxAttempts: 3, maxDelayMs: 80_000 }, "file config read with default plateau");
		assert.deepEqual(
			loadGoalSettings(dir, { PI_GOAL_NETWORK_RECOVERY_MAX_ATTEMPTS: "10", PI_GOAL_NETWORK_RECOVERY_MAX_DELAY_MS: "20000" }).networkRecovery,
			{ maxAttempts: 10, maxDelayMs: 20_000 },
			"env vars override file",
		);
	});
	assert.deepEqual(loadGoalSettings("/tmp/does-not-exist", {}).networkRecovery, {
		maxAttempts: 0,
		maxDelayMs: 80_000,
	}, "unset resolves to unbounded default policy");
});

test("project settings: task keybindings round-trip", () => {
	withTempDir((dir) => {
		writeProjectSettings(dir, { keybindings: { dashboard: {
			toggleExpand: "ctrl+shift+t",
			scrollUp: "ctrl+shift+up",
			scrollDown: "ctrl+shift+down",
		} } });
		assert.deepEqual(loadGoalSettingsFileConfig(dir).keybindings, { dashboard: {
			toggleExpand: "ctrl+shift+t",
			scrollUp: "ctrl+shift+up",
			scrollDown: "ctrl+shift+down",
		} });
	});
});

test("project settings: objectiveMaxChars persists (including 0) and clears", () => {
	withTempDir((dir) => {
		writeProjectSettings(dir, { objectiveMaxChars: 5000 });
		const loaded = loadGoalSettingsFileConfig(dir);
		assert.equal(loaded.objectiveMaxChars, 5000, "persisted value round-trips");
		writeProjectSettings(dir, { objectiveMaxChars: 0 });
		assert.equal(loadGoalSettingsFileConfig(dir).objectiveMaxChars, 0, "0 (no limit) persists explicitly");
		writeProjectSettings(dir, {});
		assert.equal(loadGoalSettingsFileConfig(dir).objectiveMaxChars, undefined, "cleared when omitted");
	});
});

test("effectiveSettingsReport: objectiveMaxChars row shows the effective value and provenance", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ objectiveMaxChars: 3000 }), "utf8");
		const lines = effectiveSettingsReport(dir, {});
		const row = lines.find((l) => l.startsWith("  max objective length"));
		assert.ok(row, "report includes the max objective length row");
		assert.match(row!, /3000 \(project\)/);
	});
});

// ── Integration: prompt suppression with settings ────────────────────────

import {
	goalPrompt,
	taskListBlock,
	verificationContractBlock,
} from "../extensions/prompts/goal-prompts.ts";
import { createGoal } from "../extensions/goal-record.ts";
import { tempDir } from "./support/fs.ts";

function goalWithTaskList(overrides: Partial<GoalRecord> & { objective?: string } = {}): GoalRecord {
	const g: GoalRecord = {
		...createGoal({ objective: overrides.objective ?? "Test goal", autoContinue: true, sisyphus: false }),
		...overrides,
	};
	return g;
}

test("taskListBlock: suppressed when disableTasks is true", () => {
	const g = goalWithTaskList();
	g.taskList = { tasks: [{ id: "t1", title: "Task 1", status: "pending" }], blockCompletion: false, proposedAt: new Date().toISOString() };
	const block = taskListBlock(g, { disableTasks: true });
	assert.equal(block, "", "should be empty when tasks disabled");
});

test("taskListBlock: present when disableTasks is false", () => {
	const g = goalWithTaskList();
	g.taskList = { tasks: [{ id: "t1", title: "Task 1", status: "pending" }], blockCompletion: false, proposedAt: new Date().toISOString() };
	const block = taskListBlock(g, { disableTasks: false });
	assert.ok(block.includes("Task 1"), "should contain task when tasks enabled");
});

test("taskListBlock: not suppressed when settings is undefined (backward compat)", () => {
	const g = goalWithTaskList();
	g.taskList = { tasks: [{ id: "t1", title: "Task 1", status: "pending" }], blockCompletion: false, proposedAt: new Date().toISOString() };
	const block = taskListBlock(g);
	assert.ok(block.includes("Task 1"), "should contain task when no settings");
});

test("verificationContractBlock: suppressed when disableContracts is true", () => {
	const g = goalWithTaskList({ verificationContract: "Must verify X" });
	const block = verificationContractBlock(g, { disableContracts: true });
	assert.equal(block, "", "should be empty when contracts disabled");
});

test("verificationContractBlock: present when disableContracts is false", () => {
	const g = goalWithTaskList({ verificationContract: "Must verify X" });
	const block = verificationContractBlock(g, { disableContracts: false });
	assert.ok(block.includes("Must verify X"), "should contain contract when contracts enabled");
});

test("verificationContractBlock: not suppressed when settings is undefined (backward compat)", () => {
	const g = goalWithTaskList({ verificationContract: "Must verify X" });
	const block = verificationContractBlock(g);
	assert.ok(block.includes("Must verify X"), "should contain contract when no settings");
});

test("goalPrompt: contract block suppressed when disableContracts is true", () => {
	const g = goalWithTaskList({ verificationContract: "Must verify X" });
	const prompt = goalPrompt(g, { disableContracts: true });
	assert.ok(!prompt.includes("VERIFICATION CONTRACT"), "contract section suppressed from goalPrompt");
});

test("goalPrompt: task list suppressed when disableTasks is true", () => {
	const g = goalWithTaskList();
	g.taskList = { tasks: [{ id: "t1", title: "Task 1", status: "pending" }], blockCompletion: false, proposedAt: new Date().toISOString() };
	const prompt = goalPrompt(g, { disableTasks: true });
	assert.ok(!prompt.includes("TASK LIST"), "task list suppressed from goalPrompt");
});

test("goalPrompt: contract block shown when settings undefined (backward compat)", () => {
	const g = goalWithTaskList({ verificationContract: "Must verify X" });
	const prompt = goalPrompt(g);
	assert.ok(prompt.includes("VERIFICATION CONTRACT"), "contract shown when no settings");
});

// ── subtaskDepth ────────────────────────────────────────────────────────

test("parseSettings: parses subtaskDepth as number", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: false, subtaskDepth: 2 });
	assert.equal(result.subtaskDepth, 2);
});

test("parseSettings: parses subtaskDepth string", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: false, subtaskDepth: "3" });
	assert.equal(result.subtaskDepth, 3);
});

test("parseSettings: rejects subtaskDepth below 1", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: false, subtaskDepth: 0 });
	assert.equal(result.subtaskDepth, undefined);
});

test("parseSettings: rejects non-numeric subtaskDepth", () => {
	const result = parseSettings({ disableTasks: false, disableContracts: false, subtaskDepth: "abc" });
	assert.equal(result.subtaskDepth, undefined);
});

test("loadGoalSettings: default subtaskDepth is 1", () => {
	withTempDir((dir) => {
		const result = loadGoalSettings(dir, {});
		assert.equal(result.subtaskDepth, 1);
	});
});

test("loadGoalSettings: reads subtaskDepth from file", () => {
	withTempDir((dir) => {
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false, subtaskDepth: 3 }), "utf8");
		const result = loadGoalSettings(dir, {});
		assert.equal(result.subtaskDepth, 3);
	});
});

// ── Scroll fix: hardware cursor toggle ──────────────────────────────────

test("loadGoalSettings respects various subtaskDepth edge cases", () => {
	withTempDir((dir) => {
		// No file = default 1
		assert.equal(loadGoalSettings(dir, {}).subtaskDepth, 1);

		// subtaskDepth 0 is rejected (below minimum), defaults to 1
		const configPath = goalSettingsPath(dir);
		fs.mkdirSync(path.dirname(configPath), { recursive: true });
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false, subtaskDepth: 0 }), "utf8");
		assert.equal(loadGoalSettings(dir, {}).subtaskDepth, 1);

		// subtaskDepth non-integer rejected
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false, subtaskDepth: 1.5 }), "utf8");
		assert.equal(loadGoalSettings(dir, {}).subtaskDepth, 1);

		// subtaskDepth negative rejected
		fs.writeFileSync(configPath, JSON.stringify({ disableTasks: false, disableContracts: false, subtaskDepth: -1 }), "utf8");
		assert.equal(loadGoalSettings(dir, {}).subtaskDepth, 1);
	});
});

// ── E2E-style: simulate goal creation with tasks ────────────────────────

test("normalizeTaskList handles subtasks", () => {
	const raw = {
		tasks: [{
			id: "t1", title: "Parent", status: "pending",
			subtasks: [
				{ id: "t1a", title: "Child", status: "pending" },
			],
		}],
		blockCompletion: false,
		proposedAt: "2026-05-27T00:00:00.000Z",
	};
	const result = normalizeTaskList(raw);
	assert.ok(result);
	assert.equal(result.tasks.length, 1);
	assert.ok(result.tasks[0]!.subtasks);
	assert.equal(result.tasks[0]!.subtasks![0]!.id, "t1a");
	assert.equal(result.tasks[0]!.subtasks![0]!.title, "Child");

	// Verify normalizeTaskItem creates proper nested structure
	const item = normalizeTaskItem({
		id: "x", title: "X", status: "pending",
		subtasks: [
			{ id: "xa", title: "XA", status: "complete" },
		],
	});
	assert.ok(item);
	assert.equal(item.subtasks?.length, 1);
	assert.equal(item.subtasks![0]!.id, "xa");
	assert.equal(item.subtasks![0]!.status, "complete");
});

test("normalizeTaskItem preserves lightweightSubtasks flag", () => {
	const item = normalizeTaskItem({
		id: "t1", title: "T1", status: "pending",
		lightweightSubtasks: true,
		subtasks: [{ id: "t1a", title: "A", status: "pending" }],
	});
	assert.ok(item);
	assert.equal(item.lightweightSubtasks, true);
	assert.equal(item.subtasks?.length, 1);
});
