|
| 1 | +/** |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 13 | + |
| 14 | +import { type HistoryEntry, loadHistory, MAX_HISTORY, pushHistory, saveHistory } from './sql-history'; |
| 15 | + |
| 16 | +const HISTORY_KEY = 'rp_sql_history_v1'; |
| 17 | + |
| 18 | +function createMemoryStorage(): Storage { |
| 19 | + const values = new Map<string, string>(); |
| 20 | + return { |
| 21 | + get length() { |
| 22 | + return values.size; |
| 23 | + }, |
| 24 | + clear: () => values.clear(), |
| 25 | + getItem: (key: string) => values.get(key) ?? null, |
| 26 | + key: (index: number) => [...values.keys()][index] ?? null, |
| 27 | + removeItem: (key: string) => values.delete(key), |
| 28 | + setItem: (key: string, value: string) => values.set(key, value), |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + vi.stubGlobal('localStorage', createMemoryStorage()); |
| 34 | +}); |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + vi.unstubAllGlobals(); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('pushHistory', () => { |
| 41 | + test('prepends the newest entry', () => { |
| 42 | + const list = pushHistory([{ sql: 'SELECT 1', at: 1 }], 'SELECT 2', 2); |
| 43 | + expect(list.map((entry) => entry.sql)).toEqual(['SELECT 2', 'SELECT 1']); |
| 44 | + }); |
| 45 | + |
| 46 | + test('dedupes an identical earlier run instead of listing it twice', () => { |
| 47 | + const list = pushHistory( |
| 48 | + [ |
| 49 | + { sql: 'SELECT 1', at: 1 }, |
| 50 | + { sql: 'SELECT 2', at: 2 }, |
| 51 | + ], |
| 52 | + 'SELECT 1', |
| 53 | + 3 |
| 54 | + ); |
| 55 | + expect(list).toEqual([ |
| 56 | + { sql: 'SELECT 1', at: 3 }, |
| 57 | + { sql: 'SELECT 2', at: 2 }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + test('caps the list length', () => { |
| 62 | + let list: HistoryEntry[] = []; |
| 63 | + for (let i = 0; i < MAX_HISTORY + 10; i += 1) { |
| 64 | + list = pushHistory(list, `SELECT ${i}`, i); |
| 65 | + } |
| 66 | + expect(list).toHaveLength(MAX_HISTORY); |
| 67 | + expect(list[0].sql).toBe(`SELECT ${MAX_HISTORY + 9}`); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('loadHistory / saveHistory', () => { |
| 72 | + test('round-trips entries through localStorage', () => { |
| 73 | + saveHistory([{ sql: 'SELECT 1', at: 42 }]); |
| 74 | + expect(loadHistory()).toEqual([{ sql: 'SELECT 1', at: 42 }]); |
| 75 | + }); |
| 76 | + |
| 77 | + test('drops malformed entries but keeps valid ones', () => { |
| 78 | + localStorage.setItem(HISTORY_KEY, JSON.stringify([{ sql: 'SELECT 1', at: 1 }, { sql: 7 }, 'nope', null])); |
| 79 | + expect(loadHistory()).toEqual([{ sql: 'SELECT 1', at: 1 }]); |
| 80 | + }); |
| 81 | + |
| 82 | + test('returns empty for a non-array value', () => { |
| 83 | + localStorage.setItem(HISTORY_KEY, JSON.stringify({ not: 'a list' })); |
| 84 | + expect(loadHistory()).toEqual([]); |
| 85 | + }); |
| 86 | + |
| 87 | + test('returns empty for invalid JSON', () => { |
| 88 | + localStorage.setItem(HISTORY_KEY, '{broken'); |
| 89 | + expect(loadHistory()).toEqual([]); |
| 90 | + }); |
| 91 | +}); |
0 commit comments