|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | + |
| 3 | +import { pipeline } from "$lib/utilities/pipeline"; |
| 4 | + |
| 5 | +import type { Store } from "$lib/types/store"; |
| 6 | +import type { WorkerState, WorkerContext } from "$lib/types/worker"; |
| 7 | + |
| 8 | +const createStore = (overrides: Partial<Store> = {}): Store => ({ |
| 9 | + l: "", |
| 10 | + n: "", |
| 11 | + u: "", |
| 12 | + c: "", |
| 13 | + s30: 0, |
| 14 | + sav: 0, |
| 15 | + aff: "", |
| 16 | + cc: 0, |
| 17 | + ...overrides |
| 18 | +}); |
| 19 | + |
| 20 | +const stores = [ |
| 21 | + createStore({ n: "A" }), |
| 22 | + createStore({ n: "B" }), |
| 23 | + createStore({ n: "C" }) |
| 24 | +]; |
| 25 | + |
| 26 | +const createContext = (overrides: Partial<WorkerContext> = {}): WorkerContext => ({ |
| 27 | + dataset: stores, |
| 28 | + keys: ["a", "b", "c"], |
| 29 | + indices: { |
| 30 | + all: [0, 1, 2], |
| 31 | + matched: [0, 1, 2] |
| 32 | + }, |
| 33 | + ...overrides |
| 34 | +}); |
| 35 | + |
| 36 | +const createState = (overrides: Partial<WorkerState> = {}): WorkerState => ({ |
| 37 | + query: { current: "" }, |
| 38 | + sort: { key: null, direction: null }, |
| 39 | + ...overrides |
| 40 | +}); |
| 41 | + |
| 42 | +describe("pipeline", () => { |
| 43 | + describe("filter", () => { |
| 44 | + it("returns existing matched indices if query has not changed", () => { |
| 45 | + const context = createContext({ indices: { all: [0, 1, 2], matched: [0] } }); |
| 46 | + const state = createState({ query: { current: "a" } }); |
| 47 | + |
| 48 | + const result = pipeline.filter("a", state, context); |
| 49 | + |
| 50 | + expect(result.filtered).toBe(false); |
| 51 | + expect(result.indices).toBe(context.indices.matched); |
| 52 | + }); |
| 53 | + |
| 54 | + it("calculates new indices if query changed", () => { |
| 55 | + const context = createContext(); |
| 56 | + const state = createState({ query: { current: "" } }); |
| 57 | + |
| 58 | + const result = pipeline.filter("b", state, context); |
| 59 | + |
| 60 | + expect(result.filtered).toBe(true); |
| 61 | + expect(result.indices).toHaveLength(1); |
| 62 | + expect(stores[result.indices[0]].n).toBe("B"); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("sort", () => { |
| 67 | + it("returns indices as is if not filtered and sort has not changed", () => { |
| 68 | + const indices = [0, 1, 2]; |
| 69 | + const state = createState({ sort: { key: "info", direction: "asc" } }); |
| 70 | + |
| 71 | + // Same sort params |
| 72 | + const result = pipeline.sort(indices, stores, { key: "info", direction: "asc" }, state, false); |
| 73 | + |
| 74 | + expect(result).toBe(indices); |
| 75 | + }); |
| 76 | + |
| 77 | + it("sorts if filtered is true even if sort params haven't changed", () => { |
| 78 | + const indices = [1, 0]; |
| 79 | + const state = createState({ sort: { key: "info", direction: "asc" } }); |
| 80 | + |
| 81 | + const result = pipeline.sort(indices, stores, { key: "info", direction: "asc" }, state, true); |
| 82 | + |
| 83 | + expect(result).not.toBe(indices); |
| 84 | + expect(result).toEqual([0, 1]); |
| 85 | + }); |
| 86 | + |
| 87 | + it("sorts if sort params changed", () => { |
| 88 | + const indices = [0, 1, 2]; |
| 89 | + const state = createState({ sort: { key: "info", direction: "asc" } }); |
| 90 | + |
| 91 | + const result = pipeline.sort(indices, stores, { key: "info", direction: "desc" }, state, false); |
| 92 | + |
| 93 | + expect(result).toEqual([2, 1, 0]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("restores natural order when sort is removed", () => { |
| 97 | + const context = createContext(); |
| 98 | + // indices: [0, 1, 2] (A, B, C) |
| 99 | + |
| 100 | + // 1. Sort descending |
| 101 | + const state = createState({ sort: { key: null, direction: null } }); |
| 102 | + |
| 103 | + // Apply sort |
| 104 | + const sortedIndices = pipeline.sort( |
| 105 | + context.indices.matched, |
| 106 | + stores, |
| 107 | + { key: "info", direction: "desc" }, |
| 108 | + state, |
| 109 | + false |
| 110 | + ); |
| 111 | + |
| 112 | + expect(sortedIndices).toEqual([2, 1, 0]); // C, B, A |
| 113 | + |
| 114 | + // Update state (simulating worker) |
| 115 | + state.sort.key = "info"; |
| 116 | + state.sort.direction = "desc"; |
| 117 | + |
| 118 | + // 2. Remove sort |
| 119 | + const currentIndices = [...sortedIndices]; |
| 120 | + |
| 121 | + const unsortedIndices = pipeline.sort( |
| 122 | + currentIndices, |
| 123 | + stores, |
| 124 | + undefined, |
| 125 | + state, |
| 126 | + false |
| 127 | + ); |
| 128 | + |
| 129 | + expect(unsortedIndices).toEqual([0, 1, 2]); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("paginate", () => { |
| 134 | + it("returns slice of dataset based on indices", () => { |
| 135 | + const indices = [2, 0]; // C, A |
| 136 | + const result = pipeline.paginate(indices, stores, 0, 10); |
| 137 | + |
| 138 | + expect(result).toHaveLength(2); |
| 139 | + expect(result[0].n).toBe("C"); |
| 140 | + expect(result[1].n).toBe("A"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("handles offset and limit", () => { |
| 144 | + const indices = [0, 1, 2]; |
| 145 | + const result = pipeline.paginate(indices, stores, 1, 1); |
| 146 | + |
| 147 | + expect(result).toHaveLength(1); |
| 148 | + expect(result[0].n).toBe("B"); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments