import assert from "node:assert/strict";
import { test } from "vitest";
import {
	buildUokProgressEvent,
	UOK_PROGRESS_EVENT_TYPES,
	validateUokProgressEvent,
} from "../uok/progress-event.js";

test("buildUokProgressEvent_when_unit_selected_returns_stable_machine_event", () => {
	assert.deepEqual(
		buildUokProgressEvent({
			ts: "2026-05-08T17:00:00.000Z",
			eventType: "unit_selected",
			unitType: "execute-task",
			unitId: "M001/S01/T01",
			role: "worker",
			sessionId: "session-1",
			workerSessionId: "worker-1",
			traceId: "trace-1",
			data: { source: "dispatch" },
		}),
		{
			schemaVersion: 1,
			ts: "2026-05-08T17:00:00.000Z",
			eventType: "unit_selected",
			unitType: "execute-task",
			unitId: "M001/S01/T01",
			role: "worker",
			sessionId: "session-1",
			workerSessionId: "worker-1",
			traceId: "trace-1",
			data: { source: "dispatch" },
		},
	);
});

test("validateUokProgressEvent_when_event_type_unknown_reports_allowed_types", () => {
	const result = validateUokProgressEvent({
		eventType: "invented_event",
		data: {},
	});

	assert.equal(result.ok, false);
	assert.match(result.issues[0], /eventType must be one of/);
	assert.match(result.issues[0], /unit_selected/);
	assert.match(result.issues[0], /unit_handoff_recorded/);
});

test("buildUokProgressEvent_when_payload_data_is_not_object_rejects_event", () => {
	assert.throws(
		() =>
			buildUokProgressEvent({
				eventType: "worker_started",
				data: "not-object",
			}),
		/Invalid UOK progress event: data must be an object/,
	);
});

test("progressEventTypes_include_droid_value_events_without_concrete_model_config", () => {
	assert.ok(UOK_PROGRESS_EVENT_TYPES.includes("model_auto_resolved"));
	assert.ok(UOK_PROGRESS_EVENT_TYPES.includes("validation_started"));
	assert.ok(UOK_PROGRESS_EVENT_TYPES.includes("unit_handoff_recorded"));
	assert.equal(
		UOK_PROGRESS_EVENT_TYPES.some((type) => type.includes("custom:")),
		false,
	);
});
