/**
 * Schedule field in headless query output.
 *
 * Purpose: verify that `sf headless query` JSON includes a schedule field
 * with due and upcoming entries.
 *
 * Consumer: CI test runner (vitest).
 */
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { test } from "vitest";

const __dirname = dirname(fileURLToPath(import.meta.url));
const src = readFileSync(join(__dirname, "..", "headless-query.ts"), "utf-8");

test("headless-query QuerySnapshot type includes schedule field", () => {
	assert.ok(
		src.includes("schedule?:"),
		"QuerySnapshot should have an optional schedule field",
	);
});

test("headless-query schedule field has pending_count and overdue_count", () => {
	const scheduleBlock = src.slice(src.indexOf("schedule?:"));
	assert.ok(
		scheduleBlock.includes("pending_count:"),
		"schedule should have pending_count",
	);
	assert.ok(
		scheduleBlock.includes("overdue_count:"),
		"schedule should have overdue_count",
	);
});

test("headless-query buildQuerySnapshot computes pending_count and overdue_count", () => {
	assert.ok(
		src.includes("pending_count:"),
		"buildQuerySnapshot should compute pending_count",
	);
	assert.ok(
		src.includes("overdue_count:"),
		"buildQuerySnapshot should compute overdue_count",
	);
	assert.ok(
		src.includes("readEntries") && src.includes('status === "pending"'),
		"buildQuerySnapshot should read entries and filter by pending status",
	);
});

test("headless-query buildQuerySnapshot populates schedule entries", () => {
	assert.ok(
		src.includes("scheduleEntries") || src.includes("schedule:"),
		"buildQuerySnapshot should populate schedule data",
	);
	assert.ok(
		src.includes("findDue") && src.includes("findUpcoming"),
		"buildQuerySnapshot should call findDue and findUpcoming",
	);
});
