|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { filterEphemeralKindsFromFilter, filterForCache, isEphemeralKind } from "./filter"; |
| 3 | +import type { NDKFilter, NDKSubscription } from "../subscription/index.js"; |
| 4 | + |
| 5 | +describe("isEphemeralKind", () => { |
| 6 | + it("should return true for kinds in the 20000-29999 range", () => { |
| 7 | + expect(isEphemeralKind(20000)).toBe(true); |
| 8 | + expect(isEphemeralKind(20001)).toBe(true); |
| 9 | + expect(isEphemeralKind(25000)).toBe(true); |
| 10 | + expect(isEphemeralKind(29999)).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should return false for kinds outside the ephemeral range", () => { |
| 14 | + expect(isEphemeralKind(0)).toBe(false); |
| 15 | + expect(isEphemeralKind(1)).toBe(false); |
| 16 | + expect(isEphemeralKind(19999)).toBe(false); |
| 17 | + expect(isEphemeralKind(30000)).toBe(false); |
| 18 | + expect(isEphemeralKind(10002)).toBe(false); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("filterEphemeralKindsFromFilter", () => { |
| 23 | + it("should return the original filter if no kinds are specified", () => { |
| 24 | + const filter: NDKFilter = { authors: ["pubkey1"] }; |
| 25 | + const result = filterEphemeralKindsFromFilter(filter); |
| 26 | + expect(result).toEqual(filter); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should return the original filter if no ephemeral kinds are present", () => { |
| 30 | + const filter: NDKFilter = { kinds: [1, 3, 10002], authors: ["pubkey1"] }; |
| 31 | + const result = filterEphemeralKindsFromFilter(filter); |
| 32 | + expect(result).toEqual(filter); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should filter out ephemeral kinds while keeping non-ephemeral ones", () => { |
| 36 | + const filter: NDKFilter = { kinds: [1, 20001, 3, 25000], authors: ["pubkey1"] }; |
| 37 | + const result = filterEphemeralKindsFromFilter(filter); |
| 38 | + expect(result).toEqual({ kinds: [1, 3], authors: ["pubkey1"] }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return null if all kinds are ephemeral", () => { |
| 42 | + const filter: NDKFilter = { kinds: [20000, 20001, 25000], authors: ["pubkey1"] }; |
| 43 | + const result = filterEphemeralKindsFromFilter(filter); |
| 44 | + expect(result).toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should return null for filters with only ephemeral kinds (edge case: exactly 20000)", () => { |
| 48 | + const filter: NDKFilter = { kinds: [20000] }; |
| 49 | + const result = filterEphemeralKindsFromFilter(filter); |
| 50 | + expect(result).toBeNull(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return null for filters with only ephemeral kinds (edge case: exactly 29999)", () => { |
| 54 | + const filter: NDKFilter = { kinds: [29999] }; |
| 55 | + const result = filterEphemeralKindsFromFilter(filter); |
| 56 | + expect(result).toBeNull(); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("filterForCache", () => { |
| 61 | + // Create a minimal mock subscription |
| 62 | + const createMockSubscription = (filters: NDKFilter[], cacheUnconstrainFilter?: string[]): NDKSubscription => ({ |
| 63 | + filters, |
| 64 | + cacheUnconstrainFilter, |
| 65 | + } as unknown as NDKSubscription); |
| 66 | + |
| 67 | + it("should remove ephemeral kinds from filters", () => { |
| 68 | + const subscription = createMockSubscription([ |
| 69 | + { kinds: [1, 20001], authors: ["pubkey1"] }, |
| 70 | + ]); |
| 71 | + const result = filterForCache(subscription); |
| 72 | + expect(result).toEqual([{ kinds: [1], authors: ["pubkey1"] }]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should remove filters where all kinds are ephemeral", () => { |
| 76 | + const subscription = createMockSubscription([ |
| 77 | + { kinds: [20001, 25000], authors: ["pubkey1"] }, |
| 78 | + { kinds: [1, 3], authors: ["pubkey2"] }, |
| 79 | + ]); |
| 80 | + const result = filterForCache(subscription); |
| 81 | + expect(result).toEqual([{ kinds: [1, 3], authors: ["pubkey2"] }]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should return empty array if all filters only have ephemeral kinds", () => { |
| 85 | + const subscription = createMockSubscription([ |
| 86 | + { kinds: [20001], authors: ["pubkey1"] }, |
| 87 | + { kinds: [25000], authors: ["pubkey2"] }, |
| 88 | + ]); |
| 89 | + const result = filterForCache(subscription); |
| 90 | + expect(result).toEqual([]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should handle cacheUnconstrainFilter by removing specified keys", () => { |
| 94 | + const subscription = createMockSubscription( |
| 95 | + [{ kinds: [1], authors: ["pubkey1"], limit: 10, since: 1234567890 }], |
| 96 | + ["limit", "since"] |
| 97 | + ); |
| 98 | + const result = filterForCache(subscription); |
| 99 | + expect(result).toEqual([{ kinds: [1], authors: ["pubkey1"] }]); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle both cacheUnconstrainFilter and ephemeral filtering", () => { |
| 103 | + const subscription = createMockSubscription( |
| 104 | + [{ kinds: [1, 20001], authors: ["pubkey1"], limit: 10 }], |
| 105 | + ["limit"] |
| 106 | + ); |
| 107 | + const result = filterForCache(subscription); |
| 108 | + expect(result).toEqual([{ kinds: [1], authors: ["pubkey1"] }]); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should not break filters without kinds specified", () => { |
| 112 | + const subscription = createMockSubscription([ |
| 113 | + { authors: ["pubkey1"] }, |
| 114 | + { ids: ["eventid1"] }, |
| 115 | + ]); |
| 116 | + const result = filterForCache(subscription); |
| 117 | + expect(result).toEqual([ |
| 118 | + { authors: ["pubkey1"] }, |
| 119 | + { ids: ["eventid1"] }, |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should handle empty kinds array", () => { |
| 124 | + const subscription = createMockSubscription([ |
| 125 | + { kinds: [], authors: ["pubkey1"] }, |
| 126 | + ]); |
| 127 | + const result = filterForCache(subscription); |
| 128 | + expect(result).toEqual([{ kinds: [], authors: ["pubkey1"] }]); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should filter out filters that become empty after cacheUnconstrainFilter", () => { |
| 132 | + const subscription = createMockSubscription( |
| 133 | + [{ limit: 10 }], |
| 134 | + ["limit"] |
| 135 | + ); |
| 136 | + const result = filterForCache(subscription); |
| 137 | + expect(result).toEqual([]); |
| 138 | + }); |
| 139 | +}); |
0 commit comments