Spaces:
Running
Running
File size: 6,690 Bytes
db8cd68 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import { beforeEach, describe, expect, it, vi } from "vitest";
const createTestEntry = (overrides: Record<string, unknown> = {}) => ({
id: 1,
searchRunId: "test-1",
query: "test",
timestamp: Date.now(),
...overrides,
});
describe("History Module - Search Run ID Management", () => {
beforeEach(async () => {
vi.resetModules();
const { resetSearchRunId } = await import("./history");
resetSearchRunId();
});
it("should generate a new search run ID when none exists", async () => {
const { getCurrentSearchRunId } = await import("./history");
const id = getCurrentSearchRunId();
expect(id).toBeDefined();
expect(typeof id).toBe("string");
expect(id.length).toBeGreaterThan(0);
});
it("should return the same ID on subsequent calls", async () => {
const { getCurrentSearchRunId } = await import("./history");
const id1 = getCurrentSearchRunId();
const id2 = getCurrentSearchRunId();
expect(id1).toBe(id2);
});
it("should allow setting a custom search run ID", async () => {
const { getCurrentSearchRunId, setCurrentSearchRunId, resetSearchRunId } =
await import("./history");
resetSearchRunId();
const customId = "custom-test-id-12345";
setCurrentSearchRunId(customId);
expect(getCurrentSearchRunId()).toBe(customId);
});
it("should allow resetting the search run ID", async () => {
const { getCurrentSearchRunId, setCurrentSearchRunId, resetSearchRunId } =
await import("./history");
resetSearchRunId();
setCurrentSearchRunId("test-id");
expect(getCurrentSearchRunId()).toBe("test-id");
resetSearchRunId();
const newId = getCurrentSearchRunId();
expect(newId).not.toBe("test-id");
expect(newId).toBeDefined();
});
it("should generate unique IDs after reset", async () => {
const { getCurrentSearchRunId, resetSearchRunId } = await import(
"./history"
);
resetSearchRunId();
const id1 = getCurrentSearchRunId();
resetSearchRunId();
const id2 = getCurrentSearchRunId();
expect(id1).not.toBe(id2);
});
});
describe("History Module - Entry Helper Functions", () => {
it("should detect text results in new structure", async () => {
const { hasTextResults } = await import("./history");
const entry = {
id: 1,
searchRunId: "test-1",
query: "test",
timestamp: Date.now(),
textResults: { type: "text" as const, items: [] },
};
expect(hasTextResults(entry)).toBe(true);
});
it("should detect text results in legacy structure", async () => {
const { hasTextResults } = await import("./history");
const entry = createTestEntry({
results: { type: "text" as const, items: [] },
});
expect(hasTextResults(entry)).toBe(true);
});
it("should return false when no text results exist", async () => {
const { hasTextResults } = await import("./history");
const entry = createTestEntry();
expect(hasTextResults(entry)).toBe(false);
});
it("should detect image results in new structure", async () => {
const { hasImageResults } = await import("./history");
const entry = createTestEntry({
imageResults: { type: "image" as const, items: [] },
});
expect(hasImageResults(entry)).toBe(true);
});
it("should detect image results in legacy structure", async () => {
const { hasImageResults } = await import("./history");
const entry = createTestEntry({
results: { type: "image" as const, items: [] },
});
expect(hasImageResults(entry)).toBe(true);
});
it("should return false when no image results exist", async () => {
const { hasImageResults } = await import("./history");
const entry = createTestEntry();
expect(hasImageResults(entry)).toBe(false);
});
it("should get results from new textResults field", async () => {
const { getResultsFromEntry } = await import("./history");
const textResults = {
type: "text" as const,
items: [
{ title: "Test", url: "https://test.com", snippet: "Test snippet" },
],
};
const entry = createTestEntry({ textResults });
expect(getResultsFromEntry(entry)).toBe(textResults);
});
it("should get results from new imageResults field", async () => {
const { getResultsFromEntry } = await import("./history");
const imageResults = {
type: "image" as const,
items: [
{
title: "Image",
url: "https://img.com/img.jpg",
thumbnail: "https://img.com/thumb.jpg",
},
],
};
const entry = createTestEntry({ imageResults });
expect(getResultsFromEntry(entry)).toBe(imageResults);
});
it("should fallback to legacy results field", async () => {
const { getResultsFromEntry } = await import("./history");
const legacyResults = {
type: "text" as const,
items: [
{
title: "Legacy",
url: "https://legacy.com",
snippet: "Legacy snippet",
},
],
};
const entry = createTestEntry({ results: legacyResults });
expect(getResultsFromEntry(entry)).toBe(legacyResults);
});
it("should return null when no results exist", async () => {
const { getResultsFromEntry } = await import("./history");
const entry = createTestEntry();
expect(getResultsFromEntry(entry)).toBeNull();
});
});
describe("Search run ID management", () => {
it("should generate new ID when none exists", async () => {
const { getCurrentSearchRunId, resetSearchRunId } = await import(
"./history"
);
resetSearchRunId();
const id = getCurrentSearchRunId();
expect(id).toBeTruthy();
expect(id).toMatch(/^\d+-[a-z0-9]+$/);
});
it("should return same ID on subsequent calls", async () => {
const { getCurrentSearchRunId, resetSearchRunId } = await import(
"./history"
);
resetSearchRunId();
const id1 = getCurrentSearchRunId();
const id2 = getCurrentSearchRunId();
expect(id1).toBe(id2);
});
it("should allow setting custom ID", async () => {
const { getCurrentSearchRunId, setCurrentSearchRunId, resetSearchRunId } =
await import("./history");
resetSearchRunId();
setCurrentSearchRunId("custom-id-123");
expect(getCurrentSearchRunId()).toBe("custom-id-123");
});
it("should reset ID to null", async () => {
const { getCurrentSearchRunId, setCurrentSearchRunId, resetSearchRunId } =
await import("./history");
resetSearchRunId();
setCurrentSearchRunId("custom-id-123");
resetSearchRunId();
const id = getCurrentSearchRunId();
expect(id).not.toBe("custom-id-123");
expect(id).toMatch(/^\d+-[a-z0-9]+$/);
});
});
|