11import { logger } from "@coder/logger"
2- import { readFile , writeFile , stat } from "fs/promises"
2+ import { readFile , writeFile , stat , utimes } from "fs/promises"
33import { Heart , heartbeatTimer } from "../../../src/node/heart"
44import { clean , mockLogger , tmpdir } from "../../utils/helpers"
55
@@ -33,17 +33,26 @@ describe("Heart", () => {
3333 const pathToFile = `${ testDir } /file.txt`
3434 await writeFile ( pathToFile , text )
3535 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+ // Docs: https://nodejs.org/api/fs.html#fspromisesutimespath-atime-mtime
40+ await utimes ( pathToFile , 0 , 0 )
41+
3742 expect ( fileContents ) . toBe ( text )
3843
3944 heart = new Heart ( pathToFile , mockIsActive ( true ) )
4045 heart . beat ( )
46+ // HACK@jsjoeio - beat has some async logic but is not an async method
47+ // Therefore, we have to create an artificial wait in order to make sure
48+ // all async code has completed before asserting
49+ await new Promise ( ( r ) => setTimeout ( r , 100 ) )
4150 // Check that the heart wrote to the heartbeatFilePath and overwrote our text
4251 const fileContentsAfterBeat = await readFile ( pathToFile , { encoding : "utf8" } )
4352 expect ( fileContentsAfterBeat ) . not . toBe ( text )
4453 // Make sure the modified timestamp was updated.
4554 const fileStatusAfterEdit = await stat ( pathToFile )
46- expect ( fileStatusAfterEdit . mtimeMs ) . toBeGreaterThan ( fileStatusBeforeEdit . mtimeMs )
55+ expect ( fileStatusAfterEdit . mtimeMs ) . toBeGreaterThan ( 0 )
4756 } )
4857 it ( "should log a warning when given an invalid file path" , async ( ) => {
4958 heart = new Heart ( `fakeDir/fake.txt` , mockIsActive ( false ) )
@@ -52,7 +61,6 @@ describe("Heart", () => {
5261 // Therefore, we have to create an artificial wait in order to make sure
5362 // all async code has completed before asserting
5463 await new Promise ( ( r ) => setTimeout ( r , 100 ) )
55- // expect(logger.trace).toHaveBeenCalled()
5664 expect ( logger . warn ) . toHaveBeenCalled ( )
5765 } )
5866 it ( "should be active after calling beat" , ( ) => {
0 commit comments