1import { beforeEach, describe, expect, it, vi, type Mock } from "vitest"
2
3import { hasLiveClips, useBoothStore, type Shot } from "@/stores/booth"
4
5/* SH-01/SH-02: irisan Strip Hidup di store. Dua hal yang dijaga di sini:
6 bingkai klip dilepas saat fotonya diganti atau sesi diulang (memori tidak
7 menumpuk), dan `hasLiveClips` menjadi gerbang kartu Hias. */
8
9function klip(jumlah: number): { frames: ImageBitmap[]; spies: Mock[] } {
10 const spies = Array.from({ length: jumlah }, () => vi.fn())
11 const frames = spies.map((spy) => ({ close: spy }) as unknown as ImageBitmap)
12 return { frames, spies }
13}
14
15function shot(clip?: ImageBitmap[]): Shot {
16 return { still: "data:image/jpeg;base64,AAAA", capturedAt: 1, clip }
17}
18
19describe("irisan Strip Hidup", () => {
20 beforeEach(() => {
21 useBoothStore.getState().reset()
22 })
23
24 it("belum ada klip sebelum ada foto berklip", () => {
25 const store = useBoothStore.getState()
26 expect(hasLiveClips(store.shots)).toBe(false)
27 store.setLayout("strip-4")
28 expect(hasLiveClips(useBoothStore.getState().shots)).toBe(false)
29 expect(useBoothStore.getState().live.enabled).toBe(false)
30 })
31
32 it("mengenali klip di slot mana pun", () => {
33 const store = useBoothStore.getState()
34 store.setLayout("strip-4")
35 store.setShot(0, shot())
36 expect(hasLiveClips(useBoothStore.getState().shots)).toBe(false)
37
38 store.setShot(2, shot(klip(10).frames))
39 expect(hasLiveClips(useBoothStore.getState().shots)).toBe(true)
40
41 store.setLiveEnabled(true)
42 expect(useBoothStore.getState().live.enabled).toBe(true)
43 })
44
45 it("melepas bingkai slot yang diganti", () => {
46 const store = useBoothStore.getState()
47 store.setLayout("strip-4")
48 const lama = klip(3)
49 store.setShot(0, shot(lama.frames))
50
51 store.setShot(0, shot())
52 for (const spy of lama.spies) {
53 expect(spy).toHaveBeenCalledTimes(1)
54 }
55 expect(hasLiveClips(useBoothStore.getState().shots)).toBe(false)
56 })
57
58 it("melepas semua bingkai saat layout diganti dan sesi diulang", () => {
59 const store = useBoothStore.getState()
60 store.setLayout("strip-4")
61 const pertama = klip(2)
62 const kedua = klip(2)
63 store.setShot(0, shot(pertama.frames))
64 store.setShot(1, shot(kedua.frames))
65 store.setLiveEnabled(true)
66
67 store.setLayout("strip-3")
68 for (const spy of [...pertama.spies, ...kedua.spies]) {
69 expect(spy).toHaveBeenCalledTimes(1)
70 }
71 expect(useBoothStore.getState().live.enabled).toBe(false)
72
73 const ketiga = klip(1)
74 store.setShot(0, shot(ketiga.frames))
75 store.setLiveEnabled(true)
76 store.reset()
77 expect(ketiga.spies[0]).toHaveBeenCalledTimes(1)
78 expect(useBoothStore.getState().live.enabled).toBe(false)
79 })
80})
81