|
| 1 | +import Keyv from 'keyv'; |
| 2 | +import KeyvPostgres from '@keyv/postgres'; |
| 3 | +import { StorageNamespace } from './storage.service'; |
| 4 | +import { PgMockManager } from '../../test/utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Integration tests using PGlite to verify the regexp_replace touch SQL |
| 8 | + * preserves stored data while updating only the expires timestamp. |
| 9 | + * |
| 10 | + * Uses beforeAll (not beforeEach) because KeyvPostgres triggers async PGlite |
| 11 | + * initialization that must complete within an awaited context. |
| 12 | + */ |
| 13 | +describe('touchPostgres regexp_replace integration (PGlite)', () => { |
| 14 | + let store: KeyvPostgres; |
| 15 | + let keyv: Keyv; |
| 16 | + const ttl = 86400000; |
| 17 | + const namespace = StorageNamespace.SCENES; |
| 18 | + |
| 19 | + const getDb = () => PgMockManager.getInstance().getDb(); |
| 20 | + |
| 21 | + /** Run the same regexp_replace SQL that touchPostgres uses. */ |
| 22 | + const runTouchSql = async (fullKey: string, expires: number) => { |
| 23 | + return getDb().query( |
| 24 | + `UPDATE keyv |
| 25 | + SET value = regexp_replace(value, '"expires":\\d+', '"expires":' || $1::text) |
| 26 | + WHERE key = $2`, |
| 27 | + [expires, fullKey], |
| 28 | + ); |
| 29 | + }; |
| 30 | + |
| 31 | + /** Read the raw value column from the keyv table. */ |
| 32 | + const readRawRow = async (fullKey: string): Promise<string | undefined> => { |
| 33 | + const result = await getDb().query<{ value: string }>( |
| 34 | + 'SELECT value FROM keyv WHERE key = $1', |
| 35 | + [fullKey], |
| 36 | + ); |
| 37 | + return result.rows[0]?.value; |
| 38 | + }; |
| 39 | + |
| 40 | + beforeAll(async () => { |
| 41 | + store = new KeyvPostgres(); |
| 42 | + keyv = new Keyv({ store, namespace, ttl }); |
| 43 | + // Force PGlite table creation by performing an initial operation |
| 44 | + await keyv.set('__init__', 'init'); |
| 45 | + await keyv.delete('__init__'); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + await PgMockManager.getInstance().clearDatabase(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterAll(async () => { |
| 53 | + await store.disconnect(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should preserve binary data after touch', async () => { |
| 57 | + const original = Buffer.from([0x00, 0x01, 0xff, 0xfe, 0x80, 0x7f]); |
| 58 | + await keyv.set('binary-key', original); |
| 59 | + |
| 60 | + const fullKey = `${namespace}:binary-key`; |
| 61 | + const newExpires = Date.now() + ttl * 2; |
| 62 | + await runTouchSql(fullKey, newExpires); |
| 63 | + |
| 64 | + const retrieved = await keyv.get('binary-key'); |
| 65 | + expect(Buffer.isBuffer(retrieved)).toBe(true); |
| 66 | + expect(Buffer.from(retrieved).equals(original)).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should update the expires timestamp in the raw row', async () => { |
| 70 | + await keyv.set('ts-key', 'hello'); |
| 71 | + |
| 72 | + const fullKey = `${namespace}:ts-key`; |
| 73 | + const rawBefore = await readRawRow(fullKey); |
| 74 | + const expiresBefore = JSON.parse(rawBefore!).expires as number; |
| 75 | + |
| 76 | + const newExpires = expiresBefore + 99999; |
| 77 | + await runTouchSql(fullKey, newExpires); |
| 78 | + |
| 79 | + const rawAfter = await readRawRow(fullKey); |
| 80 | + const expiresAfter = JSON.parse(rawAfter!).expires as number; |
| 81 | + |
| 82 | + expect(expiresAfter).toBe(newExpires); |
| 83 | + expect(expiresAfter).not.toBe(expiresBefore); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should preserve string data after touch', async () => { |
| 87 | + await keyv.set('str-key', 'some important string value'); |
| 88 | + |
| 89 | + const fullKey = `${namespace}:str-key`; |
| 90 | + await runTouchSql(fullKey, Date.now() + ttl * 2); |
| 91 | + |
| 92 | + const retrieved = await keyv.get('str-key'); |
| 93 | + expect(retrieved).toBe('some important string value'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should not modify value when expires is null', async () => { |
| 97 | + const fullKey = `${namespace}:no-expires`; |
| 98 | + // Insert a row manually with "expires":null — won't match \d+ |
| 99 | + await getDb().query(`INSERT INTO keyv (key, value) VALUES ($1, $2)`, [ |
| 100 | + fullKey, |
| 101 | + '{"value":"test-data","expires":null}', |
| 102 | + ]); |
| 103 | + |
| 104 | + const rawBefore = await readRawRow(fullKey); |
| 105 | + await runTouchSql(fullKey, Date.now() + ttl); |
| 106 | + const rawAfter = await readRawRow(fullKey); |
| 107 | + |
| 108 | + expect(rawAfter).toBe(rawBefore); |
| 109 | + }); |
| 110 | +}); |
0 commit comments