@@ -13,77 +13,61 @@ import {
1313 TWO_HOURS_IN_MS ,
1414} from "../../../../src/common/time" ;
1515import { mockedObject } from "../../utils/mocking.helpers" ;
16+ import { DirResult } from "tmp" ;
1617
17- describe ( "query history scrubber" , ( ) => {
18- const now = Date . now ( ) ;
18+ const now = Date . now ( ) ;
19+ // We don't want our times to align exactly with the hour,
20+ // so we can better mimic real life
21+ const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000 ;
1922
23+ describe ( "query history scrubber" , ( ) => {
2024 let deregister : vscode . Disposable | undefined ;
21- let mockCtx : vscode . ExtensionContext ;
22- let runCount = 0 ;
23-
24- // We don't want our times to align exactly with the hour,
25- // so we can better mimic real life
26- const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000 ;
27- const tmpDir = dirSync ( {
28- unsafeCleanup : true ,
29- } ) ;
25+ let tmpDir : DirResult ;
3026
3127 beforeEach ( ( ) => {
28+ tmpDir = dirSync ( {
29+ unsafeCleanup : true ,
30+ } ) ;
31+
3232 jest . spyOn ( extLogger , "log" ) . mockResolvedValue ( undefined ) ;
3333
3434 jest . useFakeTimers ( {
3535 doNotFake : [ "setTimeout" ] ,
3636 now,
3737 } ) ;
38-
39- mockCtx = {
40- globalState : {
41- lastScrubTime : now ,
42- get ( key : string ) {
43- if ( key !== "lastScrubTime" ) {
44- throw new Error ( `Unexpected key: ${ key } ` ) ;
45- }
46- return this . lastScrubTime ;
47- } ,
48- async update ( key : string , value : any ) {
49- if ( key !== "lastScrubTime" ) {
50- throw new Error ( `Unexpected key: ${ key } ` ) ;
51- }
52- this . lastScrubTime = value ;
53- } ,
54- } ,
55- } as any as vscode . ExtensionContext ;
5638 } ) ;
5739
5840 afterEach ( ( ) => {
5941 if ( deregister ) {
6042 deregister . dispose ( ) ;
6143 deregister = undefined ;
6244 }
45+ tmpDir . removeCallback ( ) ;
6346 } ) ;
6447
6548 it ( "should not throw an error when the query directory does not exist" , async ( ) => {
66- registerScrubber ( "idontexist" ) ;
49+ const mockCtx = createMockContext ( ) ;
50+ const runCounter = registerScrubber ( "idontexist" , mockCtx ) ;
6751
6852 jest . advanceTimersByTime ( ONE_HOUR_IN_MS ) ;
6953 await wait ( ) ;
7054 // "Should not have called the scrubber"
71- expect ( runCount ) . toBe ( 0 ) ;
55+ expect ( runCounter ) . toHaveBeenCalledTimes ( 0 ) ;
7256
7357 jest . advanceTimersByTime ( ONE_HOUR_IN_MS - 1 ) ;
7458 await wait ( ) ;
7559 // "Should not have called the scrubber"
76- expect ( runCount ) . toBe ( 0 ) ;
60+ expect ( runCounter ) . toHaveBeenCalledTimes ( 0 ) ;
7761
7862 jest . advanceTimersByTime ( 1 ) ;
7963 await wait ( ) ;
8064 // "Should have called the scrubber once"
81- expect ( runCount ) . toBe ( 1 ) ;
65+ expect ( runCounter ) . toHaveBeenCalledTimes ( 1 ) ;
8266
8367 jest . advanceTimersByTime ( TWO_HOURS_IN_MS ) ;
8468 await wait ( ) ;
8569 // "Should have called the scrubber a second time"
86- expect ( runCount ) . toBe ( 2 ) ;
70+ expect ( runCounter ) . toHaveBeenCalledTimes ( 2 ) ;
8771
8872 expect ( ( mockCtx . globalState as any ) . lastScrubTime ) . toBe (
8973 now + TWO_HOURS_IN_MS * 2 ,
@@ -97,7 +81,7 @@ describe("query history scrubber", () => {
9781 TWO_HOURS_IN_MS ,
9882 THREE_HOURS_IN_MS ,
9983 ) ;
100- registerScrubber ( queryDir ) ;
84+ registerScrubber ( queryDir , createMockContext ( ) ) ;
10185
10286 jest . advanceTimersByTime ( TWO_HOURS_IN_MS ) ;
10387 await wait ( ) ;
@@ -176,7 +160,31 @@ describe("query history scrubber", () => {
176160 return `query-${ timestamp } ` ;
177161 }
178162
179- function registerScrubber ( dir : string ) {
163+ function createMockContext ( ) : vscode . ExtensionContext {
164+ return {
165+ globalState : {
166+ lastScrubTime : now ,
167+ get ( key : string ) {
168+ if ( key !== "lastScrubTime" ) {
169+ throw new Error ( `Unexpected key: ${ key } ` ) ;
170+ }
171+ return this . lastScrubTime ;
172+ } ,
173+ async update ( key : string , value : any ) {
174+ if ( key !== "lastScrubTime" ) {
175+ throw new Error ( `Unexpected key: ${ key } ` ) ;
176+ }
177+ this . lastScrubTime = value ;
178+ } ,
179+ } ,
180+ } as any as vscode . ExtensionContext ;
181+ }
182+
183+ function registerScrubber (
184+ dir : string ,
185+ ctx : vscode . ExtensionContext ,
186+ ) : jest . Mock {
187+ const onScrubberRun = jest . fn ( ) ;
180188 deregister = registerQueryHistoryScrubber (
181189 ONE_HOUR_IN_MS ,
182190 TWO_HOURS_IN_MS ,
@@ -187,11 +195,10 @@ describe("query history scrubber", () => {
187195 return Promise . resolve ( ) ;
188196 } ,
189197 } ) ,
190- mockCtx ,
191- {
192- increment : ( ) => runCount ++ ,
193- } ,
198+ ctx ,
199+ onScrubberRun ,
194200 ) ;
201+ return onScrubberRun ;
195202 }
196203
197204 async function wait ( ms = 500 ) {
0 commit comments