import { beforeEach, describe, expect, it } from "bun:test";
import { tags } from "../../src/db/schema.ts";
import { getAllTags } from "../../src/server/services/tag.service.ts";
import { createTestDb } from "../helpers/db.ts";

describe("Tag Service", () => {
	let db: Awaited<ReturnType<typeof createTestDb>>["db"];

	beforeEach(async () => {
		const testDb = await createTestDb();
		db = testDb.db;
	});

	it("returns empty array when no tags exist", async () => {
		const result = await getAllTags(db);
		expect(result).toEqual([]);
	});

	it("returns all tags as { id, name } ordered alphabetically", async () => {
		await db
			.insert(tags)
			.values([
				{ name: "bikepacking" },
				{ name: "ultralight" },
				{ name: "accessories" },
			]);

		const result = await getAllTags(db);
		expect(result).toHaveLength(3);
		expect(result[0].name).toBe("accessories");
		expect(result[1].name).toBe("bikepacking");
		expect(result[2].name).toBe("ultralight");
		// Should NOT include createdAt
		expect(result[0]).toEqual({ id: expect.any(Number), name: "accessories" });
	});
});
