// sf — Regression test: autonomous mode resume resolves resource-loader.js from deployed path (#3949)
// Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { test } from "vitest";

const __dirname = dirname(fileURLToPath(import.meta.url));
const autoJsPath = join(
	__dirname,
	"..",
	"resources",
	"extensions",
	"sf",
	"auto.js",
);
const loaderTsPath = join(__dirname, "..", "loader.ts");

test("loader.ts sets SF_PKG_ROOT env var", () => {
	const loaderSrc = readFileSync(loaderTsPath, "utf-8");
	assert.ok(
		loaderSrc.includes("process.env.SF_PKG_ROOT"),
		"loader.ts must set SF_PKG_ROOT so deployed extensions can locate package-root modules",
	);
});

test("auto.js resume uses SF_PKG_ROOT for resource-loader import, not bare relative path", () => {
	const autoSrc = readFileSync(autoJsPath, "utf-8");

	// Must reference SF_PKG_ROOT to build an absolute path
	assert.ok(
		autoSrc.includes("process.env.SF_PKG_ROOT"),
		"auto.js must use SF_PKG_ROOT to resolve resource-loader.js from deployed extension path",
	);

	// The import must use the computed variable (resourceLoaderPath), not a hardcoded relative path.
	assert.ok(
		autoSrc.includes("await import(resourceLoaderPath)"),
		"auto.js resource-loader import must use the computed resourceLoaderPath variable, not a hardcoded relative path",
	);

	// The resourceLoaderPath must be constructed from SF_PKG_ROOT via pathToFileURL
	// (raw filesystem paths break on Windows with ERR_UNSUPPORTED_ESM_URL_SCHEME)
	assert.ok(
		autoSrc.includes("pathToFileURL(join(pkgRoot,"),
		"auto.js must convert the constructed path to a file URL for cross-platform import()",
	);
});

test("SF_PKG_ROOT resolves resource-loader.js correctly from package root", () => {
	// Simulate what auto.ts does: given SF_PKG_ROOT, construct the path
	const pkgRoot = resolve(__dirname, "..", "..");
	const resourceLoaderPath = join(pkgRoot, "dist", "resource-loader.js");

	// After build, dist/resource-loader.js should exist
	// (this test runs post-build in CI; in dev it validates the path construction)
	const expectedDir = dirname(resourceLoaderPath);
	assert.ok(
		expectedDir.endsWith(join("dist")),
		`resource-loader path should be under dist/, got: ${expectedDir}`,
	);
});
