@@ -46,6 +46,57 @@ describe('instrumentContext', () => {
4646 const instrumented = instrumentContext ( context ) ;
4747 expect ( instrumented [ s ] ) . toBe ( context [ s ] ) ;
4848 } ) ;
49+
50+ describe ( 'DurableObjectState storage instrumentation' , ( ) => {
51+ it ( 'instruments storage property' , ( ) => {
52+ const mockStorage = createMockStorage ( ) ;
53+ const context = makeDurableObjectStateMock ( mockStorage ) ;
54+ const instrumented = instrumentContext ( context ) ;
55+
56+ // The storage property should be instrumented (wrapped)
57+ expect ( instrumented . storage ) . toBeDefined ( ) ;
58+ // The instrumented storage should not be the same reference
59+ expect ( instrumented . storage ) . not . toBe ( mockStorage ) ;
60+ } ) ;
61+
62+ it ( 'exposes originalStorage as the uninstrumented storage' , ( ) => {
63+ const mockStorage = createMockStorage ( ) ;
64+ const context = makeDurableObjectStateMock ( mockStorage ) ;
65+ const instrumented = instrumentContext ( context ) ;
66+
67+ // originalStorage should be the original uninstrumented storage
68+ expect ( instrumented . originalStorage ) . toBe ( mockStorage ) ;
69+ } ) ;
70+
71+ it ( 'originalStorage is not enumerable' , ( ) => {
72+ const mockStorage = createMockStorage ( ) ;
73+ const context = makeDurableObjectStateMock ( mockStorage ) ;
74+ const instrumented = instrumentContext ( context ) ;
75+
76+ // originalStorage should not appear in Object.keys
77+ expect ( Object . keys ( instrumented ) ) . not . toContain ( 'originalStorage' ) ;
78+ } ) ;
79+
80+ it ( 'returns instrumented storage lazily' , ( ) => {
81+ const mockStorage = createMockStorage ( ) ;
82+ const context = makeDurableObjectStateMock ( mockStorage ) ;
83+ const instrumented = instrumentContext ( context ) ;
84+
85+ // Access storage twice to ensure memoization
86+ const storage1 = instrumented . storage ;
87+ const storage2 = instrumented . storage ;
88+
89+ expect ( storage1 ) . toBe ( storage2 ) ;
90+ } ) ;
91+
92+ it ( 'handles context without storage property' , ( ) => {
93+ const context = makeExecutionContextMock ( ) ;
94+ const instrumented = instrumentContext ( context ) as any ;
95+
96+ // Should not have originalStorage if no storage property
97+ expect ( instrumented . originalStorage ) . toBeUndefined ( ) ;
98+ } ) ;
99+ } ) ;
49100} ) ;
50101
51102function makeExecutionContextMock < T extends ExecutionContext > ( ) {
@@ -54,3 +105,39 @@ function makeExecutionContextMock<T extends ExecutionContext>() {
54105 passThroughOnException : vi . fn ( ) ,
55106 } as unknown as Mocked < T > ;
56107}
108+
109+ function makeDurableObjectStateMock ( storage ?: any ) {
110+ return {
111+ waitUntil : vi . fn ( ) ,
112+ blockConcurrencyWhile : vi . fn ( ) ,
113+ id : { toString : ( ) => 'test-id' , equals : vi . fn ( ) , name : 'test' } ,
114+ storage : storage || createMockStorage ( ) ,
115+ acceptWebSocket : vi . fn ( ) ,
116+ getWebSockets : vi . fn ( ) . mockReturnValue ( [ ] ) ,
117+ setWebSocketAutoResponse : vi . fn ( ) ,
118+ getWebSocketAutoResponse : vi . fn ( ) ,
119+ getWebSocketAutoResponseTimestamp : vi . fn ( ) ,
120+ setHibernatableWebSocketEventTimeout : vi . fn ( ) ,
121+ getHibernatableWebSocketEventTimeout : vi . fn ( ) ,
122+ getTags : vi . fn ( ) . mockReturnValue ( [ ] ) ,
123+ abort : vi . fn ( ) ,
124+ } as any ;
125+ }
126+
127+ function createMockStorage ( ) : any {
128+ return {
129+ get : vi . fn ( ) . mockResolvedValue ( undefined ) ,
130+ put : vi . fn ( ) . mockResolvedValue ( undefined ) ,
131+ delete : vi . fn ( ) . mockResolvedValue ( false ) ,
132+ list : vi . fn ( ) . mockResolvedValue ( new Map ( ) ) ,
133+ getAlarm : vi . fn ( ) . mockResolvedValue ( null ) ,
134+ setAlarm : vi . fn ( ) . mockResolvedValue ( undefined ) ,
135+ deleteAlarm : vi . fn ( ) . mockResolvedValue ( undefined ) ,
136+ deleteAll : vi . fn ( ) . mockResolvedValue ( undefined ) ,
137+ sync : vi . fn ( ) . mockResolvedValue ( undefined ) ,
138+ transaction : vi . fn ( ) . mockImplementation ( async ( cb : ( ) => unknown ) => cb ( ) ) ,
139+ sql : {
140+ exec : vi . fn ( ) ,
141+ } ,
142+ } ;
143+ }
0 commit comments