@@ -37,7 +37,81 @@ describe("DOShardedTagCache class", () => {
3737 // @ts -expect-error - testing private method
3838 expect ( cache . ctx . blockConcurrencyWhile ) . toHaveBeenCalled ( ) ;
3939 expect ( cache . sql . exec ) . toHaveBeenCalledWith (
40- `CREATE TABLE IF NOT EXISTS revalidations (tag TEXT PRIMARY KEY, revalidatedAt INTEGER)`
40+ `CREATE TABLE IF NOT EXISTS revalidations (tag TEXT PRIMARY KEY, revalidatedAt INTEGER, stale INTEGER, expire INTEGER DEFAULT NULL )`
4141 ) ;
42+ expect ( cache . sql . exec ) . toHaveBeenCalledWith (
43+ `ALTER TABLE revalidations ADD COLUMN stale INTEGER; ALTER TABLE revalidations ADD COLUMN expire INTEGER DEFAULT NULL`
44+ ) ;
45+ } ) ;
46+
47+ describe ( "getTagData" , ( ) => {
48+ it ( "should return an empty object for empty tags" , async ( ) => {
49+ const cache = createDOShardedTagCache ( ) ;
50+ const result = await cache . getTagData ( [ ] ) ;
51+ expect ( result ) . toEqual ( { } ) ;
52+ expect ( cache . sql . exec ) . not . toHaveBeenCalledWith ( expect . stringContaining ( "SELECT" ) , expect . anything ( ) ) ;
53+ } ) ;
54+
55+ it ( "should query all columns and return a record" , async ( ) => {
56+ const cache = createDOShardedTagCache ( ) ;
57+ vi . mocked ( cache . sql . exec ) . mockReturnValueOnce ( {
58+ toArray : ( ) => [
59+ { tag : "tag1" , revalidatedAt : 1000 , stale : 1000 , expire : null } ,
60+ { tag : "tag2" , revalidatedAt : 2000 , stale : 1500 , expire : 9999 } ,
61+ ] ,
62+ } as ReturnType < SqlStorage [ "exec" ] > ) ;
63+ const result = await cache . getTagData ( [ "tag1" , "tag2" ] ) ;
64+ expect ( result ) . toEqual ( {
65+ tag1 : { revalidatedAt : 1000 , stale : 1000 , expire : null } ,
66+ tag2 : { revalidatedAt : 2000 , stale : 1500 , expire : 9999 } ,
67+ } ) ;
68+ } ) ;
69+
70+ it ( "should return empty object on SQL error" , async ( ) => {
71+ const cache = createDOShardedTagCache ( ) ;
72+ vi . mocked ( cache . sql . exec ) . mockImplementationOnce ( ( ) => {
73+ throw new Error ( "sql error" ) ;
74+ } ) ;
75+ const result = await cache . getTagData ( [ "tag1" ] ) ;
76+ expect ( result ) . toEqual ( { } ) ;
77+ } ) ;
78+ } ) ;
79+
80+ describe ( "writeTags" , ( ) => {
81+ it ( "should write string tags using the old format (backward compat)" , async ( ) => {
82+ const cache = createDOShardedTagCache ( ) ;
83+ await cache . writeTags ( [ "tag1" , "tag2" ] , 1000 ) ;
84+ expect ( cache . sql . exec ) . toHaveBeenCalledWith (
85+ `INSERT OR REPLACE INTO revalidations (tag, revalidatedAt, stale) VALUES (?, ?, ?)` ,
86+ "tag1" ,
87+ 1000 ,
88+ 1000
89+ ) ;
90+ expect ( cache . sql . exec ) . toHaveBeenCalledWith (
91+ `INSERT OR REPLACE INTO revalidations (tag, revalidatedAt, stale) VALUES (?, ?, ?)` ,
92+ "tag2" ,
93+ 1000 ,
94+ 1000
95+ ) ;
96+ } ) ;
97+
98+ it ( "should write object tags using stale and expire" , async ( ) => {
99+ const cache = createDOShardedTagCache ( ) ;
100+ await cache . writeTags ( [ { tag : "tag1" , stale : 5000 , expire : 9999 } ] ) ;
101+ expect ( cache . sql . exec ) . toHaveBeenCalledWith (
102+ `INSERT OR REPLACE INTO revalidations (tag, revalidatedAt, stale, expire) VALUES (?, ?, ?, ?)` ,
103+ "tag1" ,
104+ 5000 ,
105+ 5000 ,
106+ 9999
107+ ) ;
108+ } ) ;
109+
110+ it ( "should return early for empty tags" , async ( ) => {
111+ const cache = createDOShardedTagCache ( ) ;
112+ const execCallsBeforeCreate = vi . mocked ( cache . sql . exec ) . mock . calls . length ;
113+ await cache . writeTags ( [ ] ) ;
114+ expect ( vi . mocked ( cache . sql . exec ) . mock . calls . length ) . toBe ( execCallsBeforeCreate ) ;
115+ } ) ;
42116 } ) ;
43117} ) ;
0 commit comments