import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, test } from "vitest";

const autoSource = readFileSync(
	join(process.cwd(), "src/resources/extensions/sf/auto.js"),
	"utf-8",
);

describe("autonomous startup doctor", () => {
	test("startAuto_when_session_not_running_runs_startup_doctor_fix_before_resume_dispatch", () => {
		const runningGuard = autoSource.indexOf('classification === "running"');
		const doctorCall = autoSource.indexOf(
			"await runStartupDoctorFix(ctx, base)",
		);
		const pausedResume = autoSource.indexOf("// If resuming from paused state");

		assert.ok(runningGuard !== -1, "running-session guard must exist");
		assert.ok(doctorCall !== -1, "fresh startup must run doctor fix");
		assert.ok(pausedResume !== -1, "paused resume marker must exist");
		assert.ok(
			runningGuard < doctorCall,
			"doctor must not run while another session is active",
		);
		assert.ok(
			doctorCall < pausedResume,
			"doctor must run before resume/dispatch decisions",
		);
	});
});
