import assert from "node:assert/strict";
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";

import { deployBundleHashlineConfig } from "../extensions/hashline-config.ts";

const EXPECTED_CONFIG = {
	hashLength: 2,
	grep: false,
	replaceText: false,
};

test("hashline config deployment enforces bundle defaults", async () => {
	const agentDir = await mkdtemp(join(tmpdir(), "my-pi-hashline-"));
	const targetPath = join(agentDir, "hashline.json");

	try {
		await writeFile(targetPath, '{"grep":true}\n', "utf8");
		deployBundleHashlineConfig(agentDir);
		assert.deepEqual(JSON.parse(await readFile(targetPath, "utf8")), EXPECTED_CONFIG);

		const first = await readFile(targetPath, "utf8");
		deployBundleHashlineConfig(agentDir);
		assert.equal(await readFile(targetPath, "utf8"), first);
	} finally {
		await rm(agentDir, { recursive: true, force: true });
	}
});
