import { describe, expect, it } from "vitest"; import { teamSectionSchema } from "@/lib/guides/schemas"; import { toTeamDraft } from "@/lib/guides/team-draft"; import { cloneTeamImports } from "@/lib/guides/team-tools"; const storedTeam = () => ({ id: "stored-team", title: "Example team", rotationSeconds: "21.5", combo: "E > Q", guideName: "Source guide", conditionName: "Boss", members: [1, 2, 3, 4].map((index) => ({ characterKey: `character-${index}`, weaponKey: `weapon-${index}`, artifactKey: `artifact-${index}`, secondArtifactKey: index === 1 ? "artifact-second" : null, constellation: index === 1 ? "0-6" : "2", refinement: index === 1 ? "1-5" : "4", damage: "123456.78", characterName: `Character ${index}`, character: { name: `Character ${index}`, imageUrl: "/character.webp" }, })), }); describe("team drafts", () => { it("selects editable fields and excludes persisted IDs and hydrated metadata", () => { const source = storedTeam(); const draft = toTeamDraft(source); expect(draft).toEqual({ title: "Example team", rotationSeconds: "21.5", combo: "E > Q", members: [1, 2, 3, 4].map((index) => ({ characterKey: `character-${index}`, weaponKey: `weapon-${index}`, artifactKey: `artifact-${index}`, secondArtifactKey: index === 1 ? "artifact-second" : null, constellation: index === 1 ? "0-6" : "2", refinement: index === 1 ? "1-5" : "4", damage: "123456.78", })), }); expect({ id: source.id, ...draft }).toHaveProperty("id", "stored-team"); }); it("creates independent draft members and preserves nullable equipment", () => { const source = storedTeam(); const member = { ...source.members[0], weaponKey: null, artifactKey: null }; const input = { ...source, members: [member] }; const draft = toTeamDraft(input); const otherDraft = toTeamDraft(input); expect(draft.members[0]).toMatchObject({ weaponKey: null, artifactKey: null }); expect(draft.members).not.toBe(input.members); expect(draft.members[0]).not.toBe(member); draft.members[0].damage = "0"; expect(member.damage).toBe("123456.78"); expect(otherDraft.members[0].damage).toBe("123456.78"); }); it("imports teams with fresh IDs and produces a valid editor save payload", () => { const source = storedTeam(); const draft = toTeamDraft(source); const [imported] = cloneTeamImports([draft], () => "new-team"); expect(imported.clientId).toBe("new-team"); expect(imported.members[0]).not.toBe(draft.members[0]); const result = teamSectionSchema.parse({ expectedVersion: 7, groups: [{ name: "Boss", note: null, iconMediaId: null, teams: [{ title: imported.title, rotationSeconds: Number(imported.rotationSeconds), combo: imported.combo, members: imported.members.map((member) => ({ ...member, damage: Number(member.damage), })), }], }], }); expect(result.expectedVersion).toBe(7); expect(result.groups[0].teams[0].rotationSeconds).toBe(21.5); expect(result.groups[0].teams[0].members[0]).toMatchObject({ constellation: "0-6", refinement: "1-5", damage: 123456.78, }); }); });