<script module lang="ts">
	import { defineMeta, type StoryContext } from "@storybook/addon-svelte-csf";
	import { expect } from "storybook/test";
	import CommitList from "./CommitList.svelte";
	import type { CommitSummary } from "./types";

	const commits: CommitSummary[] = [
		{
			hash: "0123456789abcdef0123456789abcdef01234567",
			shortHash: "01234567",
			subject: "add a storybook fixture",
			body: "this body is hidden until the disclosure button is pressed.",
			authorName: "dawn",
			authorEmail: "dawn@example.test",
			when: "2026-07-28T09:00:00Z",
			changeId: "kqpuwoxz"
		},
		{
			hash: "abcdef0123456789abcdef0123456789abcdef01",
			shortHash: "abcdef01",
			subject: "document the repository view",
			body: "",
			authorName: "dawn",
			authorEmail: "dawn@example.test",
			when: "2026-07-27T09:00:00Z"
		}
	];
	const minimalCommit = { ...commits[1], authorName: "", when: "", changeId: undefined };
	type PlayContext = Pick<StoryContext<Record<string, unknown>>, "canvas" | "userEvent">;

	const expandBody = async ({ canvas, userEvent }: PlayContext) => {
		const toggle = canvas.getByRole("button", { name: "Toggle commit body" });
		await userEvent.click(toggle);
		await expect(toggle).toHaveAttribute("aria-expanded", "true");
		await expect(
			canvas.getByText("this body is hidden until the disclosure button is pressed.")
		).toBeVisible();
	};

	const { Story } = defineMeta({
		title: "Repo/CommitList",
		component: CommitList,
		tags: ["autodocs"],
		args: {
			ownerHandle: "dawn",
			repoName: "tangled",
			commits,
			tagsByCommit: {
				"0123456789abcdef0123456789abcdef01234567": ["v1.0.0"]
			}
		}
	});
</script>

<Story name="Disclosure body and tag" play={expandBody} />
<Story name="Missing optional metadata" args={{ commits: [minimalCommit] }} />
<Story name="No commits" args={{ commits: [] }} />
