|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { createPinia, setActivePinia } from "pinia"; |
| 3 | + |
| 4 | +// Activity store tests (SPEC s11.4; DESIGN s8.3). The seams are |
| 5 | +// `@tauri-apps/api/core`'s `invoke` (every typed IPC wrapper routes through it) |
| 6 | +// and `@tauri-apps/api/event`'s `listen` (the live-tail subscription). Mocking |
| 7 | +// both lets us drive the store against a fake backend + manually fire live |
| 8 | +// events, asserting: pagination appends without re-querying earlier pages, a |
| 9 | +// live event prepends + dedups, filters re-query from page 0, and the empty |
| 10 | +// state renders. |
| 11 | + |
| 12 | +const invokeMock = vi.fn(); |
| 13 | +vi.mock("@tauri-apps/api/core", () => ({ |
| 14 | + invoke: (cmd: string, args?: unknown) => invokeMock(cmd, args), |
| 15 | +})); |
| 16 | + |
| 17 | +// The live-tail seam: capture the handler `onActivityNew` registers so the test |
| 18 | +// can fire `activity:new` events on demand. `listen` returns an unlisten fn. |
| 19 | +let liveHandler: ((payload: unknown) => void) | null = null; |
| 20 | +const unlistenMock = vi.fn(); |
| 21 | +vi.mock("@tauri-apps/api/event", () => ({ |
| 22 | + listen: vi.fn((event: string, cb: (e: { payload: unknown }) => void) => { |
| 23 | + if (event === "activity:new") { |
| 24 | + liveHandler = (payload: unknown) => cb({ payload }); |
| 25 | + } |
| 26 | + return Promise.resolve(unlistenMock); |
| 27 | + }), |
| 28 | +})); |
| 29 | + |
| 30 | +import { useActivityStore, ACTIVITY_PAGE_SIZE } from "../stores/activity"; |
| 31 | +import type { ActivityEntry } from "../ipc/types"; |
| 32 | + |
| 33 | +function makeEntry(over: Partial<ActivityEntry> = {}): ActivityEntry { |
| 34 | + return { |
| 35 | + id: 1, |
| 36 | + ts: 1000, |
| 37 | + sourceId: null, |
| 38 | + level: "info", |
| 39 | + eventType: "upload_done", |
| 40 | + fileCount: null, |
| 41 | + bytes: null, |
| 42 | + message: null, |
| 43 | + ...over, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +/** Build a query_activity page DTO for `entries`, with paging metadata. */ |
| 48 | +function makePage( |
| 49 | + entries: ActivityEntry[], |
| 50 | + page: number, |
| 51 | + total: number, |
| 52 | +): { |
| 53 | + entries: ActivityEntry[]; |
| 54 | + total: number; |
| 55 | + page: number; |
| 56 | + limit: number; |
| 57 | + hasMore: boolean; |
| 58 | +} { |
| 59 | + const consumed = (page + 1) * ACTIVITY_PAGE_SIZE; |
| 60 | + return { |
| 61 | + entries, |
| 62 | + total, |
| 63 | + page, |
| 64 | + limit: ACTIVITY_PAGE_SIZE, |
| 65 | + hasMore: total > consumed, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +beforeEach(() => { |
| 70 | + setActivePinia(createPinia()); |
| 71 | + invokeMock.mockReset(); |
| 72 | + unlistenMock.mockReset(); |
| 73 | + liveHandler = null; |
| 74 | +}); |
| 75 | + |
| 76 | +describe("activity store: pagination", () => { |
| 77 | + it("loadInitial fetches page 0 and records paging metadata", async () => { |
| 78 | + const rows = [makeEntry({ id: 3 }), makeEntry({ id: 2 }), makeEntry({ id: 1 })]; |
| 79 | + invokeMock.mockResolvedValueOnce(makePage(rows, 0, 250)); |
| 80 | + const store = useActivityStore(); |
| 81 | + await store.loadInitial(); |
| 82 | + expect(invokeMock).toHaveBeenCalledWith("query_activity", { |
| 83 | + filter: {}, |
| 84 | + page: { page: 0, limit: ACTIVITY_PAGE_SIZE }, |
| 85 | + }); |
| 86 | + expect(store.entries).toHaveLength(3); |
| 87 | + expect(store.entries[0].id).toBe(3); |
| 88 | + expect(store.total).toBe(250); |
| 89 | + expect(store.hasMore).toBe(true); |
| 90 | + expect(store.loadedPage).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("loadMore appends the next page WITHOUT re-querying earlier pages", async () => { |
| 94 | + const page0 = [makeEntry({ id: 200 }), makeEntry({ id: 199 })]; |
| 95 | + const page1 = [makeEntry({ id: 100 }), makeEntry({ id: 99 })]; |
| 96 | + invokeMock.mockResolvedValueOnce(makePage(page0, 0, 250)); |
| 97 | + const store = useActivityStore(); |
| 98 | + await store.loadInitial(); |
| 99 | + expect(invokeMock).toHaveBeenCalledTimes(1); |
| 100 | + |
| 101 | + invokeMock.mockResolvedValueOnce(makePage(page1, 1, 250)); |
| 102 | + await store.loadMore(); |
| 103 | + |
| 104 | + // Exactly ONE additional fetch (page 1), never a re-fetch of page 0. |
| 105 | + expect(invokeMock).toHaveBeenCalledTimes(2); |
| 106 | + expect(invokeMock).toHaveBeenNthCalledWith(2, "query_activity", { |
| 107 | + filter: {}, |
| 108 | + page: { page: 1, limit: ACTIVITY_PAGE_SIZE }, |
| 109 | + }); |
| 110 | + // Pages accumulated client-side, in order. |
| 111 | + expect(store.entries.map((e) => e.id)).toEqual([200, 199, 100, 99]); |
| 112 | + expect(store.loadedPage).toBe(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it("loadMore is a no-op when no more pages remain", async () => { |
| 116 | + invokeMock.mockResolvedValueOnce(makePage([makeEntry({ id: 1 })], 0, 1)); |
| 117 | + const store = useActivityStore(); |
| 118 | + await store.loadInitial(); |
| 119 | + expect(store.hasMore).toBe(false); |
| 120 | + await store.loadMore(); |
| 121 | + // No second fetch fired. |
| 122 | + expect(invokeMock).toHaveBeenCalledTimes(1); |
| 123 | + }); |
| 124 | + |
| 125 | + it("loadMore dedups a row already present from the live tail", async () => { |
| 126 | + const page0 = [makeEntry({ id: 200 })]; |
| 127 | + invokeMock.mockResolvedValueOnce(makePage(page0, 0, 250)); |
| 128 | + const store = useActivityStore(); |
| 129 | + await store.subscribeLive(); |
| 130 | + await store.loadInitial(); |
| 131 | + |
| 132 | + // A live event arrives for id 150 before it is paged in. |
| 133 | + liveHandler?.(makeEntry({ id: 150, ts: 900 })); |
| 134 | + expect(store.entries.map((e) => e.id)).toEqual([150, 200]); |
| 135 | + |
| 136 | + // Page 1 includes id 150 again - it must NOT be duplicated. |
| 137 | + invokeMock.mockResolvedValueOnce( |
| 138 | + makePage([makeEntry({ id: 150, ts: 900 }), makeEntry({ id: 149 })], 1, 250), |
| 139 | + ); |
| 140 | + await store.loadMore(); |
| 141 | + const ids = store.entries.map((e) => e.id); |
| 142 | + expect(ids.filter((i) => i === 150)).toHaveLength(1); |
| 143 | + expect(ids).toEqual([150, 200, 149]); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe("activity store: live tail", () => { |
| 148 | + it("prepends a live event newest-first and bumps total", async () => { |
| 149 | + invokeMock.mockResolvedValueOnce(makePage([makeEntry({ id: 1 })], 0, 1)); |
| 150 | + const store = useActivityStore(); |
| 151 | + await store.subscribeLive(); |
| 152 | + await store.loadInitial(); |
| 153 | + expect(store.total).toBe(1); |
| 154 | + |
| 155 | + liveHandler?.(makeEntry({ id: 2, ts: 2000 })); |
| 156 | + expect(store.entries[0].id).toBe(2); |
| 157 | + expect(store.entries.map((e) => e.id)).toEqual([2, 1]); |
| 158 | + expect(store.total).toBe(2); |
| 159 | + }); |
| 160 | + |
| 161 | + it("dedups a live event whose id is already present", async () => { |
| 162 | + invokeMock.mockResolvedValueOnce(makePage([makeEntry({ id: 5 })], 0, 1)); |
| 163 | + const store = useActivityStore(); |
| 164 | + await store.subscribeLive(); |
| 165 | + await store.loadInitial(); |
| 166 | + // Same id fired live: ignored. |
| 167 | + liveHandler?.(makeEntry({ id: 5 })); |
| 168 | + expect(store.entries).toHaveLength(1); |
| 169 | + expect(store.total).toBe(1); |
| 170 | + }); |
| 171 | + |
| 172 | + it("drops a live event that does not match the active filter", async () => { |
| 173 | + invokeMock.mockResolvedValueOnce(makePage([], 0, 0)); |
| 174 | + const store = useActivityStore(); |
| 175 | + await store.subscribeLive(); |
| 176 | + await store.applyFilter({ minLevel: "error" }); |
| 177 | + // An info-level live event must be dropped under a min-level=error filter. |
| 178 | + liveHandler?.(makeEntry({ id: 9, level: "info" })); |
| 179 | + expect(store.entries).toHaveLength(0); |
| 180 | + // A matching error-level event is kept. |
| 181 | + liveHandler?.(makeEntry({ id: 10, level: "error" })); |
| 182 | + expect(store.entries.map((e) => e.id)).toEqual([10]); |
| 183 | + }); |
| 184 | + |
| 185 | + it("unsubscribeLive calls the unlisten fn", async () => { |
| 186 | + const store = useActivityStore(); |
| 187 | + await store.subscribeLive(); |
| 188 | + store.unsubscribeLive(); |
| 189 | + expect(unlistenMock).toHaveBeenCalledTimes(1); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe("activity store: filters", () => { |
| 194 | + it("applyFilter re-queries from page 0 with the new filter and resets state", async () => { |
| 195 | + invokeMock.mockResolvedValueOnce( |
| 196 | + makePage([makeEntry({ id: 1 }), makeEntry({ id: 2 })], 0, 2), |
| 197 | + ); |
| 198 | + const store = useActivityStore(); |
| 199 | + await store.loadInitial(); |
| 200 | + expect(store.entries).toHaveLength(2); |
| 201 | + |
| 202 | + invokeMock.mockResolvedValueOnce( |
| 203 | + makePage([makeEntry({ id: 3, level: "error" })], 0, 1), |
| 204 | + ); |
| 205 | + await store.applyFilter({ minLevel: "error", sourceId: "src-1" }); |
| 206 | + |
| 207 | + expect(invokeMock).toHaveBeenNthCalledWith(2, "query_activity", { |
| 208 | + filter: { minLevel: "error", sourceId: "src-1" }, |
| 209 | + page: { page: 0, limit: ACTIVITY_PAGE_SIZE }, |
| 210 | + }); |
| 211 | + // Old rows cleared; only the re-queried page remains. |
| 212 | + expect(store.entries.map((e) => e.id)).toEqual([3]); |
| 213 | + expect(store.loadedPage).toBe(0); |
| 214 | + }); |
| 215 | +}); |
| 216 | + |
| 217 | +describe("activity store: empty state", () => { |
| 218 | + it("isEmpty is true after loading an empty page", async () => { |
| 219 | + invokeMock.mockResolvedValueOnce(makePage([], 0, 0)); |
| 220 | + const store = useActivityStore(); |
| 221 | + await store.loadInitial(); |
| 222 | + expect(store.entries).toHaveLength(0); |
| 223 | + expect(store.isEmpty).toBe(true); |
| 224 | + expect(store.error).toBeNull(); |
| 225 | + }); |
| 226 | + |
| 227 | + it("isEmpty is false when an error occurred", async () => { |
| 228 | + invokeMock.mockRejectedValueOnce(new Error("db locked")); |
| 229 | + const store = useActivityStore(); |
| 230 | + await store.loadInitial(); |
| 231 | + expect(store.error).toContain("db locked"); |
| 232 | + expect(store.isEmpty).toBe(false); |
| 233 | + }); |
| 234 | +}); |
0 commit comments