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

const {
	derivePendingWorkflowCommandLabel,
	executeWorkflowActionInPowerMode,
	navigateToSFView,
} = await import("../../../web/lib/workflow-action-execution.ts");

test("derivePendingWorkflowCommandLabel prefers the latest input line while a command is in flight", () => {
	const label = derivePendingWorkflowCommandLabel({
		commandInFlight: "prompt",
		terminalLines: [
			{ id: "1", timestamp: "12:00", type: "system", content: "Bridge ready" },
			{ id: "2", timestamp: "12:01", type: "input", content: "/sf" },
			{ id: "3", timestamp: "12:02", type: "system", content: "Working…" },
		],
	});

	assert.equal(label, "/sf");
});

test("derivePendingWorkflowCommandLabel falls back to the command type when no input line exists", () => {
	const label = derivePendingWorkflowCommandLabel({
		commandInFlight: "abort",
		terminalLines: [],
	});

	assert.equal(label, "/abort");
});

test("navigateToSFView dispatches the shared browser navigation event", (_t) => {
	const originalWindow = (globalThis as { window?: EventTarget }).window;
	const fakeWindow = new EventTarget();
	const seen: string[] = [];

	fakeWindow.addEventListener("sf:navigate-view", (event: Event) => {
		seen.push((event as CustomEvent<{ view: string }>).detail.view);
	});

	(globalThis as { window?: EventTarget }).window = fakeWindow;

	afterEach(() => {
		(globalThis as { window?: EventTarget }).window = originalWindow;
	});

	navigateToSFView("power");

	assert.deepEqual(seen, ["power"]);
});

test("executeWorkflowActionInPowerMode calls dispatch and navigates to the appropriate view", async (_t) => {
	const originalWindow = (globalThis as { window?: EventTarget }).window;
	const originalLocalStorage = (globalThis as any).localStorage;
	const fakeWindow = new EventTarget();
	const seenViews: string[] = [];
	let dispatchCalled = false;

	fakeWindow.addEventListener("sf:navigate-view", (event: Event) => {
		seenViews.push((event as CustomEvent<{ view: string }>).detail.view);
	});

	(globalThis as { window?: EventTarget }).window = fakeWindow;
	(globalThis as any).localStorage = { getItem: () => null, setItem: () => {} };

	afterEach(() => {
		(globalThis as { window?: EventTarget }).window = originalWindow;
		(globalThis as any).localStorage = originalLocalStorage;
	});

	executeWorkflowActionInPowerMode({
		dispatch: async () => {
			dispatchCalled = true;
		},
	});
	// dispatch is fire-and-forget, give it a tick to resolve
	await new Promise((resolve) => setTimeout(resolve, 10));

	assert.equal(dispatchCalled, true, "dispatch should have been called");
	assert.ok(seenViews.length > 0, "should navigate to a view");
});
