hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { |
| 2 | DEFAULT_FLOATING_BUTTON, |
| 3 | radialOffsets, |
| 4 | readFloatingButton, |
| 5 | writeFloatingButton, |
| 6 | } from "@/lib/floating-button"; |
| 7 | |
| 8 | // The node project has no DOM storage; a Map-backed stand-in is enough here. |
| 9 | function stubStorage() { |
| 10 | const store = new Map<string, string>(); |
| 11 | vi.stubGlobal("window", {}); |
| 12 | vi.stubGlobal("localStorage", { |
| 13 | getItem: (key: string) => store.get(key) ?? null, |
| 14 | setItem: (key: string, value: string) => void store.set(key, value), |
| 15 | clear: () => store.clear(), |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | describe("floating button state", () => { |
| 20 | beforeEach(stubStorage); |
| 21 | afterEach(() => vi.unstubAllGlobals()); |
| 22 | |
| 23 | it("starts bottom-right and unparked", () => { |
| 24 | expect(readFloatingButton()).toEqual(DEFAULT_FLOATING_BUTTON); |
| 25 | }); |
| 26 | |
| 27 | it("round-trips a saved spot", () => { |
| 28 | writeFloatingButton({ x: 0.2, y: 0.4, parked: true }); |
| 29 | expect(readFloatingButton()).toEqual({ x: 0.2, y: 0.4, parked: true }); |
| 30 | }); |
| 31 | |
| 32 | it("clamps out-of-range shares and ignores junk", () => { |
| 33 | localStorage.setItem("hezarfen.floatingButton", JSON.stringify({ x: 3, y: "top", parked: "yes" })); |
| 34 | expect(readFloatingButton()).toEqual({ x: 1, y: 1, parked: false }); |
| 35 | localStorage.setItem("hezarfen.floatingButton", "{not json"); |
| 36 | expect(readFloatingButton()).toEqual(DEFAULT_FLOATING_BUTTON); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe("radialOffsets", () => { |
| 41 | const R = 80; |
| 42 | |
| 43 | it("fans a bottom-right button up and to the left only", () => { |
| 44 | const offsets = radialOffsets(3, 0.95, 0.95, R); |
| 45 | for (const { dx, dy } of offsets) { |
| 46 | expect(dx).toBeLessThanOrEqual(1e-9); |
| 47 | expect(dy).toBeLessThanOrEqual(1e-9); |
| 48 | } |
| 49 | // Quarter circle: the ends sit straight left and straight up. |
| 50 | expect(offsets[0].dx).toBeCloseTo(-R); |
| 51 | expect(offsets[2].dy).toBeCloseTo(-R); |
| 52 | }); |
| 53 | |
| 54 | it("fans a top-left button down and to the right only", () => { |
| 55 | for (const { dx, dy } of radialOffsets(3, 0.05, 0.05, R)) { |
| 56 | expect(dx).toBeGreaterThanOrEqual(-1e-9); |
| 57 | expect(dy).toBeGreaterThanOrEqual(-1e-9); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | it("fans away from a side edge", () => { |
| 62 | for (const { dx } of radialOffsets(3, 0.02, 0.5, R)) expect(dx).toBeGreaterThan(0); |
| 63 | }); |
| 64 | |
| 65 | it("opens upwards from the middle of the screen", () => { |
| 66 | const [only] = radialOffsets(1, 0.5, 0.5, R); |
| 67 | expect(only.dx).toBeCloseTo(0); |
| 68 | expect(only.dy).toBeCloseTo(-R); |
| 69 | }); |
| 70 | |
| 71 | it("keeps every item at the radius", () => { |
| 72 | for (const { dx, dy } of radialOffsets(4, 0.3, 0.8, R)) expect(Math.hypot(dx, dy)).toBeCloseTo(R); |
| 73 | expect(radialOffsets(0, 0.5, 0.5, R)).toEqual([]); |
| 74 | }); |
| 75 | }); |