1
1
import { logger } from "@coder/logger"
2
- import { readFile , writeFile , stat } from "fs/promises"
2
+ import { readFile , writeFile , stat , open } from "fs/promises"
3
3
import { Heart , heartbeatTimer } from "../../../src/node/heart"
4
4
import { clean , mockLogger , tmpdir } from "../../utils/helpers"
5
5
@@ -33,7 +33,12 @@ describe("Heart", () => {
33
33
const pathToFile = `${ testDir } /file.txt`
34
34
await writeFile ( pathToFile , text )
35
35
const fileContents = await readFile ( pathToFile , { encoding : "utf8" } )
36
- const fileStatusBeforeEdit = await stat ( pathToFile )
36
+ // Explicitly set the modified time to 0 so that we can check
37
+ // that the file was indeed modified after calling heart.beat().
38
+ // This works around any potential race conditions.
39
+ const fileHandle = await open ( pathToFile , "r+" )
40
+ await fileHandle . utimes ( 0 , 0 )
41
+
37
42
expect ( fileContents ) . toBe ( text )
38
43
39
44
heart = new Heart ( pathToFile , mockIsActive ( true ) )
@@ -47,7 +52,7 @@ describe("Heart", () => {
47
52
expect ( fileContentsAfterBeat ) . not . toBe ( text )
48
53
// Make sure the modified timestamp was updated.
49
54
const fileStatusAfterEdit = await stat ( pathToFile )
50
- expect ( fileStatusAfterEdit . mtimeMs ) . toBeGreaterThanOrEqual ( fileStatusBeforeEdit . mtimeMs )
55
+ expect ( fileStatusAfterEdit . mtimeMs ) . toBeGreaterThan ( 0 )
51
56
} )
52
57
it ( "should log a warning when given an invalid file path" , async ( ) => {
53
58
heart = new Heart ( `fakeDir/fake.txt` , mockIsActive ( false ) )
0 commit comments