From 40be3e1afbb54f06ddeeb121199f3902faa1d460 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 15:37:10 +0530 Subject: [PATCH 01/14] feat: cache latest `fuels` version --- .changeset/kind-chairs-move.md | 5 ++ .../utils/checkForAndDisplayUpdates.test.ts | 3 +- .../cli/utils/checkForAndDisplayUpdates.ts | 14 +--- .../fuels/src/cli/utils/fuelsVersionCache.ts | 29 +++++++++ .../cli/utils/getLatestFuelsVersion.test.ts | 64 +++++++++++++++++++ .../src/cli/utils/getLatestFuelsVersion.ts | 32 ++++++++++ packages/fuels/src/run.ts | 3 +- 7 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 .changeset/kind-chairs-move.md create mode 100644 packages/fuels/src/cli/utils/fuelsVersionCache.ts create mode 100644 packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts create mode 100644 packages/fuels/src/cli/utils/getLatestFuelsVersion.ts diff --git a/.changeset/kind-chairs-move.md b/.changeset/kind-chairs-move.md new file mode 100644 index 00000000000..e0e855e1dbd --- /dev/null +++ b/.changeset/kind-chairs-move.md @@ -0,0 +1,5 @@ +--- +"fuels": patch +--- + +feat: cache latest `fuels` version diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts index 107027e9d59..542e26a3b33 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.test.ts @@ -1,6 +1,7 @@ import * as versionsMod from '@fuel-ts/versions'; import * as checkForAndDisplayUpdatesMod from './checkForAndDisplayUpdates'; +import * as getLatestFuelsVersionMod from './getLatestFuelsVersion'; import * as loggerMod from './logger'; /** @@ -17,7 +18,7 @@ describe('checkForAndDisplayUpdates', () => { const mockDeps = (params: { latestVersion: string; userVersion: string }) => { const { latestVersion, userVersion } = params; - vi.spyOn(Promise, 'race').mockReturnValue(Promise.resolve(latestVersion)); + vi.spyOn(getLatestFuelsVersionMod, 'getLatestFuelsVersion').mockResolvedValue(latestVersion); vi.spyOn(versionsMod, 'versions', 'get').mockReturnValue({ FUELS: userVersion, diff --git a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts index 32294045b2e..5abb67681d0 100644 --- a/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts +++ b/packages/fuels/src/cli/utils/checkForAndDisplayUpdates.ts @@ -1,23 +1,13 @@ import { versions, gt, eq } from '@fuel-ts/versions'; +import { getLatestFuelsVersion } from './getLatestFuelsVersion'; import { warn, log } from './logger'; -export const getLatestFuelsVersion = async () => { - const response = await fetch('https://registry.npmjs.org/fuels/latest'); - const data = await response.json(); - return data.version as string; -}; - export const checkForAndDisplayUpdates = async () => { try { const { FUELS: userFuelsVersion } = versions; - const latestFuelsVersion = await Promise.race([ - new Promise((resolve) => { - setTimeout(resolve, 3000); - }), - getLatestFuelsVersion(), - ]); + const latestFuelsVersion = await getLatestFuelsVersion(); if (!latestFuelsVersion) { log(`\n Unable to fetch latest fuels version. Skipping...\n`); diff --git a/packages/fuels/src/cli/utils/fuelsVersionCache.ts b/packages/fuels/src/cli/utils/fuelsVersionCache.ts new file mode 100644 index 00000000000..801240c60fa --- /dev/null +++ b/packages/fuels/src/cli/utils/fuelsVersionCache.ts @@ -0,0 +1,29 @@ +import fs from 'fs'; +import path from 'path'; + +export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, '.fuels-cache.json'); + +export type FuelsVersionCache = { + data: { version: string } | null; + timestamp: number; +}; + +export const saveToCache = (cache: FuelsVersionCache) => { + fs.writeFileSync(FUELS_VERSION_CACHE_FILE, JSON.stringify(cache), 'utf-8'); +}; + +export const FUELS_VERSION_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours in milliseconds + +export const checkAndLoadCache = (): FuelsVersionCache | null => { + if (fs.existsSync(FUELS_VERSION_CACHE_FILE)) { + const savedCache = JSON.parse(fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8')); + if ( + savedCache && + savedCache.data && + Date.now() - savedCache.timestamp < FUELS_VERSION_CACHE_TTL + ) { + return savedCache.data.version; + } + } + return null; +}; diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts new file mode 100644 index 00000000000..eb224a8f779 --- /dev/null +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -0,0 +1,64 @@ +import * as cacheMod from './fuelsVersionCache'; +import { getLatestFuelsVersion } from './getLatestFuelsVersion'; + +describe('getLatestFuelsVersion', () => { + it('should fail if fetch fails', async () => { + vi.spyOn(global, 'fetch').mockImplementation(() => + Promise.reject(new Error('Failed to fetch')) + ); + await expect(getLatestFuelsVersion()).rejects.toThrowError('Failed to fetch'); + }); + + it('should throw if fetch times out', async () => { + vi.spyOn(global, 'fetch').mockImplementation( + () => + new Promise((resolve) => { + setTimeout(resolve, 5000); + }) + ); + await expect(getLatestFuelsVersion()).rejects.toThrow(); + }); + + it('should return cached version if it exists', async () => { + const cache = { + data: { + version: '1.0.0', + }, + timestamp: Date.now(), + }; + vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache); + const result = await getLatestFuelsVersion(); + expect(result).toEqual('1.0.0'); + }); + + it('should fetch if there is no cache, and save to cache', async () => { + const mockResponse = new Response(JSON.stringify({ version: '1.0.0' })); + const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue(Promise.resolve(mockResponse)); + const saveCacheSpy = vi.spyOn(cacheMod, 'saveToCache').mockImplementation(() => {}); + vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(null); + const version = await getLatestFuelsVersion(); + expect(fetchSpy).toHaveBeenCalled(); + expect(version).toEqual('1.0.0'); + expect(saveCacheSpy).toHaveBeenCalledWith({ + data: { + version: '1.0.0', + }, + timestamp: expect.any(Number), + }); + }); + + it('should refetch if the cache is expired', async () => { + const cache = { + data: { + version: '0.0.0', + }, + timestamp: Date.now() - cacheMod.FUELS_VERSION_CACHE_TTL - 1000, + }; + vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache); + const mockResponse = new Response(JSON.stringify({ version: '1.0.0' })); + const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue(Promise.resolve(mockResponse)); + const version = await getLatestFuelsVersion(); + expect(fetchSpy).toHaveBeenCalled(); + expect(version).toEqual('1.0.0'); + }); +}); diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts new file mode 100644 index 00000000000..5f5d1655928 --- /dev/null +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts @@ -0,0 +1,32 @@ +import { checkAndLoadCache, FUELS_VERSION_CACHE_TTL, saveToCache } from './fuelsVersionCache'; + +export const getLatestFuelsVersion = async (): Promise => { + const now = Date.now(); + + const cache = checkAndLoadCache(); + if (cache !== null && cache.data !== null && now - cache.timestamp < FUELS_VERSION_CACHE_TTL) { + return cache.data.version; + } + + const data: { version: string } | null = await Promise.race([ + new Promise((resolve) => { + setTimeout(() => resolve(null), 3000); + }), + fetch('https://registry.npmjs.org/fuels/latest').then((response) => response.json()), + ]); + + if (!data) { + throw new Error('Failed to fetch latest fuels version.'); + } + + const version = data.version as string; + + saveToCache({ + data: { + version, + }, + timestamp: now, + }); + + return version; +}; diff --git a/packages/fuels/src/run.ts b/packages/fuels/src/run.ts index 2fadc25ad0f..387080ebcd9 100644 --- a/packages/fuels/src/run.ts +++ b/packages/fuels/src/run.ts @@ -3,6 +3,7 @@ import { checkForAndDisplayUpdates } from './cli/utils/checkForAndDisplayUpdates import { error } from './cli/utils/logger'; export const run = async (argv: string[]) => { + await checkForAndDisplayUpdates().catch(error); const program = configureCli(); - return Promise.all([await checkForAndDisplayUpdates().catch(error), program.parseAsync(argv)]); + return program.parseAsync(argv); }; From a4c4d964d20dbd24ee3d58b9f8128998b50d2348 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 15:37:30 +0530 Subject: [PATCH 02/14] add testing group --- packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts index eb224a8f779..785556bef09 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -1,6 +1,9 @@ import * as cacheMod from './fuelsVersionCache'; import { getLatestFuelsVersion } from './getLatestFuelsVersion'; +/** + * @group node + */ describe('getLatestFuelsVersion', () => { it('should fail if fetch fails', async () => { vi.spyOn(global, 'fetch').mockImplementation(() => From fd2b0206a354683e81b759d420cdb6c3904d95b9 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 16:14:16 +0530 Subject: [PATCH 03/14] remove extra check Co-authored-by: Peter Smith --- packages/fuels/src/cli/utils/getLatestFuelsVersion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts index 5f5d1655928..20d6d3a2eae 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts @@ -4,7 +4,7 @@ export const getLatestFuelsVersion = async (): Promise => { const now = Date.now(); const cache = checkAndLoadCache(); - if (cache !== null && cache.data !== null && now - cache.timestamp < FUELS_VERSION_CACHE_TTL) { + if (cache !== null) { return cache.data.version; } From 6b2984eb2ac36003fa32ce22b14810cd95dc5363 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 16:18:35 +0530 Subject: [PATCH 04/14] fix logic --- packages/fuels/src/cli/utils/getLatestFuelsVersion.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts index 20d6d3a2eae..b18d76ec290 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts @@ -1,10 +1,10 @@ -import { checkAndLoadCache, FUELS_VERSION_CACHE_TTL, saveToCache } from './fuelsVersionCache'; +import { checkAndLoadCache, saveToCache } from './fuelsVersionCache'; export const getLatestFuelsVersion = async (): Promise => { const now = Date.now(); const cache = checkAndLoadCache(); - if (cache !== null) { + if (cache && cache.data) { return cache.data.version; } From 9f96cf100796871c0f05d79d0ff0bd7bc8a82192 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 16:53:47 +0530 Subject: [PATCH 05/14] fix test --- .../src/cli/utils/getLatestFuelsVersion.test.ts | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts index 785556bef09..30fc68bd8a6 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -34,7 +34,7 @@ describe('getLatestFuelsVersion', () => { expect(result).toEqual('1.0.0'); }); - it('should fetch if there is no cache, and save to cache', async () => { + it('should fetch if there is no cache or the cache is expired', async () => { const mockResponse = new Response(JSON.stringify({ version: '1.0.0' })); const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue(Promise.resolve(mockResponse)); const saveCacheSpy = vi.spyOn(cacheMod, 'saveToCache').mockImplementation(() => {}); @@ -49,19 +49,4 @@ describe('getLatestFuelsVersion', () => { timestamp: expect.any(Number), }); }); - - it('should refetch if the cache is expired', async () => { - const cache = { - data: { - version: '0.0.0', - }, - timestamp: Date.now() - cacheMod.FUELS_VERSION_CACHE_TTL - 1000, - }; - vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache); - const mockResponse = new Response(JSON.stringify({ version: '1.0.0' })); - const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue(Promise.resolve(mockResponse)); - const version = await getLatestFuelsVersion(); - expect(fetchSpy).toHaveBeenCalled(); - expect(version).toEqual('1.0.0'); - }); }); From 8829edf1ebc6129efa0fcafc18501413ea252a11 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 19 Sep 2024 18:05:05 +0530 Subject: [PATCH 06/14] remove data from type --- packages/fuels/src/cli/utils/fuelsVersionCache.ts | 2 +- .../fuels/src/cli/utils/getLatestFuelsVersion.test.ts | 8 ++------ packages/fuels/src/cli/utils/getLatestFuelsVersion.ts | 8 +++----- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/fuels/src/cli/utils/fuelsVersionCache.ts b/packages/fuels/src/cli/utils/fuelsVersionCache.ts index 801240c60fa..8c13df9b52e 100644 --- a/packages/fuels/src/cli/utils/fuelsVersionCache.ts +++ b/packages/fuels/src/cli/utils/fuelsVersionCache.ts @@ -4,7 +4,7 @@ import path from 'path'; export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, '.fuels-cache.json'); export type FuelsVersionCache = { - data: { version: string } | null; + version: string; timestamp: number; }; diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts index 30fc68bd8a6..1d2375d3282 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -24,9 +24,7 @@ describe('getLatestFuelsVersion', () => { it('should return cached version if it exists', async () => { const cache = { - data: { - version: '1.0.0', - }, + version: '1.0.0', timestamp: Date.now(), }; vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache); @@ -43,9 +41,7 @@ describe('getLatestFuelsVersion', () => { expect(fetchSpy).toHaveBeenCalled(); expect(version).toEqual('1.0.0'); expect(saveCacheSpy).toHaveBeenCalledWith({ - data: { - version: '1.0.0', - }, + version: '1.0.0', timestamp: expect.any(Number), }); }); diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts index b18d76ec290..50f408c0cf6 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts @@ -4,8 +4,8 @@ export const getLatestFuelsVersion = async (): Promise => { const now = Date.now(); const cache = checkAndLoadCache(); - if (cache && cache.data) { - return cache.data.version; + if (cache) { + return cache.version; } const data: { version: string } | null = await Promise.race([ @@ -22,9 +22,7 @@ export const getLatestFuelsVersion = async (): Promise => { const version = data.version as string; saveToCache({ - data: { - version, - }, + version, timestamp: now, }); From fcfc05adf339f1a26e5ea590eeb6f22b5741234f Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 20 Sep 2024 12:47:13 +0530 Subject: [PATCH 07/14] use a plan-text version file instead of json --- .../fuels/src/cli/utils/fuelsVersionCache.ts | 20 +++++++------------ .../cli/utils/getLatestFuelsVersion.test.ts | 12 +++-------- .../src/cli/utils/getLatestFuelsVersion.ts | 13 ++++-------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/packages/fuels/src/cli/utils/fuelsVersionCache.ts b/packages/fuels/src/cli/utils/fuelsVersionCache.ts index 8c13df9b52e..c69fa336801 100644 --- a/packages/fuels/src/cli/utils/fuelsVersionCache.ts +++ b/packages/fuels/src/cli/utils/fuelsVersionCache.ts @@ -1,28 +1,22 @@ import fs from 'fs'; import path from 'path'; -export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, '.fuels-cache.json'); +export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, 'FUELS_VERSION'); -export type FuelsVersionCache = { - version: string; - timestamp: number; -}; +export type FuelsVersionCache = string; export const saveToCache = (cache: FuelsVersionCache) => { - fs.writeFileSync(FUELS_VERSION_CACHE_FILE, JSON.stringify(cache), 'utf-8'); + fs.writeFileSync(FUELS_VERSION_CACHE_FILE, cache, 'utf-8'); }; export const FUELS_VERSION_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours in milliseconds export const checkAndLoadCache = (): FuelsVersionCache | null => { if (fs.existsSync(FUELS_VERSION_CACHE_FILE)) { - const savedCache = JSON.parse(fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8')); - if ( - savedCache && - savedCache.data && - Date.now() - savedCache.timestamp < FUELS_VERSION_CACHE_TTL - ) { - return savedCache.data.version; + const cachedVersion = fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8'); + const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE); + if (cachedVersion && Date.now() - cacheTimestamp < FUELS_VERSION_CACHE_TTL) { + return cachedVersion; } } return null; diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts index 1d2375d3282..3e05847a0a9 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -23,11 +23,8 @@ describe('getLatestFuelsVersion', () => { }); it('should return cached version if it exists', async () => { - const cache = { - version: '1.0.0', - timestamp: Date.now(), - }; - vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cache); + const cachedVersion = '1.0.0'; + vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cachedVersion); const result = await getLatestFuelsVersion(); expect(result).toEqual('1.0.0'); }); @@ -40,9 +37,6 @@ describe('getLatestFuelsVersion', () => { const version = await getLatestFuelsVersion(); expect(fetchSpy).toHaveBeenCalled(); expect(version).toEqual('1.0.0'); - expect(saveCacheSpy).toHaveBeenCalledWith({ - version: '1.0.0', - timestamp: expect.any(Number), - }); + expect(saveCacheSpy).toHaveBeenCalledWith('1.0.0'); }); }); diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts index 50f408c0cf6..9082eccdc21 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.ts @@ -1,11 +1,9 @@ import { checkAndLoadCache, saveToCache } from './fuelsVersionCache'; export const getLatestFuelsVersion = async (): Promise => { - const now = Date.now(); - - const cache = checkAndLoadCache(); - if (cache) { - return cache.version; + const cachedVersion = checkAndLoadCache(); + if (cachedVersion) { + return cachedVersion; } const data: { version: string } | null = await Promise.race([ @@ -21,10 +19,7 @@ export const getLatestFuelsVersion = async (): Promise => { const version = data.version as string; - saveToCache({ - version, - timestamp: now, - }); + saveToCache(version); return version; }; From 0da9888e53164f5ee4efce7d4c30bb974e6a7261 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Wed, 25 Sep 2024 21:53:19 +0530 Subject: [PATCH 08/14] properly reset mocks --- .../fuels/src/cli/utils/getLatestFuelsVersion.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts index 3e05847a0a9..4a7c9dd909b 100644 --- a/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts +++ b/packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts @@ -5,6 +5,14 @@ import { getLatestFuelsVersion } from './getLatestFuelsVersion'; * @group node */ describe('getLatestFuelsVersion', () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + it('should fail if fetch fails', async () => { vi.spyOn(global, 'fetch').mockImplementation(() => Promise.reject(new Error('Failed to fetch')) From 30e917953775e057bac7ba0adcdf1fda05c5d76c Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 27 Sep 2024 16:20:48 +0530 Subject: [PATCH 09/14] fix tests failing --- packages/fuels/src/cli.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/fuels/src/cli.test.ts b/packages/fuels/src/cli.test.ts index 129769e12b9..5a230ec449e 100644 --- a/packages/fuels/src/cli.test.ts +++ b/packages/fuels/src/cli.test.ts @@ -2,6 +2,7 @@ import { Command } from 'commander'; import * as cliMod from './cli'; import { Commands } from './cli/types'; +import * as checkForUpdatesMod from './cli/utils/checkForAndDisplayUpdates'; import * as loggingMod from './cli/utils/logger'; import { run } from './run'; @@ -77,6 +78,9 @@ describe('cli.js', () => { .mockReturnValue(Promise.resolve(command)); const configureCli = vi.spyOn(cliMod, 'configureCli').mockImplementation(() => new Command()); + vi.spyOn(checkForUpdatesMod, 'checkForAndDisplayUpdates').mockImplementation(() => + Promise.resolve() + ); await run([]); From 92d2c56d4e5fcf419bd641566b5779aba8c2c312 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 27 Sep 2024 17:47:55 +0530 Subject: [PATCH 10/14] update mock logic --- packages/fuels/src/cli/config/loadConfig.test.ts | 5 +++++ packages/fuels/test/features/build.test.ts | 5 +++++ packages/fuels/test/features/deploy.test.ts | 5 +++++ packages/fuels/test/features/dev.test.ts | 5 +++++ packages/fuels/test/features/init.test.ts | 2 ++ packages/fuels/test/utils/mockCheckForUpdates.ts | 7 +++++++ 6 files changed, 29 insertions(+) create mode 100644 packages/fuels/test/utils/mockCheckForUpdates.ts diff --git a/packages/fuels/src/cli/config/loadConfig.test.ts b/packages/fuels/src/cli/config/loadConfig.test.ts index 857f2b6e8df..47e5fd84800 100644 --- a/packages/fuels/src/cli/config/loadConfig.test.ts +++ b/packages/fuels/src/cli/config/loadConfig.test.ts @@ -1,6 +1,7 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; +import { mockCheckForUpdates } from '../../../test/utils/mockCheckForUpdates'; import { runInit, bootstrapProject, @@ -19,6 +20,10 @@ import { loadConfig } from './loadConfig'; describe('loadConfig', () => { const paths = bootstrapProject(__filename); + beforeEach(() => { + mockCheckForUpdates(); + }); + afterEach(() => { resetConfigAndMocks(paths.fuelsConfigPath); }); diff --git a/packages/fuels/test/features/build.test.ts b/packages/fuels/test/features/build.test.ts index 98af3bd6d38..07ba32c8f40 100644 --- a/packages/fuels/test/features/build.test.ts +++ b/packages/fuels/test/features/build.test.ts @@ -3,6 +3,7 @@ import { join } from 'path'; import * as deployMod from '../../src/cli/commands/deploy/index'; import { mockStartFuelCore } from '../utils/mockAutoStartFuelCore'; +import { mockCheckForUpdates } from '../utils/mockCheckForUpdates'; import { bootstrapProject, resetConfigAndMocks, @@ -17,6 +18,10 @@ import { describe('build', { timeout: 180000 }, () => { const paths = bootstrapProject(__filename); + beforeEach(() => { + mockCheckForUpdates(); + }); + afterEach(() => { resetConfigAndMocks(paths.fuelsConfigPath); }); diff --git a/packages/fuels/test/features/deploy.test.ts b/packages/fuels/test/features/deploy.test.ts index 55a2e7f9f00..9b811f9d324 100644 --- a/packages/fuels/test/features/deploy.test.ts +++ b/packages/fuels/test/features/deploy.test.ts @@ -5,6 +5,7 @@ import { existsSync, readFileSync, writeFileSync } from 'fs'; import { join } from 'path'; import { launchTestNode } from '../../src/test-utils'; +import { mockCheckForUpdates } from '../utils/mockCheckForUpdates'; import { resetDiskAndMocks } from '../utils/resetDiskAndMocks'; import { bootstrapProject, @@ -21,6 +22,10 @@ describe('deploy', { timeout: 180000 }, () => { let paths = bootstrapProject(__filename); beforeEach(() => { + mockCheckForUpdates(); + }); + + afterAll(() => { resetConfigAndMocks(paths.fuelsConfigPath); resetDiskAndMocks(paths.root); paths = bootstrapProject(__filename); diff --git a/packages/fuels/test/features/dev.test.ts b/packages/fuels/test/features/dev.test.ts index 832e7917432..b8aa63231f6 100644 --- a/packages/fuels/test/features/dev.test.ts +++ b/packages/fuels/test/features/dev.test.ts @@ -3,6 +3,7 @@ import * as chokidar from 'chokidar'; import * as buildMod from '../../src/cli/commands/build/index'; import * as deployMod from '../../src/cli/commands/deploy/index'; import { mockStartFuelCore } from '../utils/mockAutoStartFuelCore'; +import { mockCheckForUpdates } from '../utils/mockCheckForUpdates'; import { mockLogger } from '../utils/mockLogger'; import { resetDiskAndMocks } from '../utils/resetDiskAndMocks'; import { runInit, runDev, bootstrapProject, resetConfigAndMocks } from '../utils/runCommands'; @@ -21,6 +22,10 @@ vi.mock('chokidar', async () => { describe('dev', () => { const paths = bootstrapProject(__filename); + beforeEach(() => { + mockCheckForUpdates(); + }); + afterEach(() => { resetConfigAndMocks(paths.fuelsConfigPath); }); diff --git a/packages/fuels/test/features/init.test.ts b/packages/fuels/test/features/init.test.ts index 05095c9a9f3..93b9fd72b7e 100644 --- a/packages/fuels/test/features/init.test.ts +++ b/packages/fuels/test/features/init.test.ts @@ -2,6 +2,7 @@ import chalk from 'chalk'; import { existsSync, readFileSync } from 'fs'; import { Commands } from '../../src'; +import { mockCheckForUpdates } from '../utils/mockCheckForUpdates'; import { mockLogger } from '../utils/mockLogger'; import { bootstrapProject, @@ -19,6 +20,7 @@ describe('init', () => { beforeEach(() => { mockLogger(); + mockCheckForUpdates(); }); afterEach(() => { diff --git a/packages/fuels/test/utils/mockCheckForUpdates.ts b/packages/fuels/test/utils/mockCheckForUpdates.ts new file mode 100644 index 00000000000..23f0afd0a98 --- /dev/null +++ b/packages/fuels/test/utils/mockCheckForUpdates.ts @@ -0,0 +1,7 @@ +import * as checkForUpdatesMod from '../../src/cli/utils/checkForAndDisplayUpdates'; + +export const mockCheckForUpdates = () => { + vi.spyOn(checkForUpdatesMod, 'checkForAndDisplayUpdates').mockImplementation(() => + Promise.resolve() + ); +}; From 7b07793d68a5361fc8c9e78942d5a461ee62046b Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 27 Sep 2024 18:07:31 +0530 Subject: [PATCH 11/14] fix merge conflict --- packages/fuels/test/features/deploy.test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/fuels/test/features/deploy.test.ts b/packages/fuels/test/features/deploy.test.ts index 9b811f9d324..8dc0f57b03f 100644 --- a/packages/fuels/test/features/deploy.test.ts +++ b/packages/fuels/test/features/deploy.test.ts @@ -23,9 +23,6 @@ describe('deploy', { timeout: 180000 }, () => { beforeEach(() => { mockCheckForUpdates(); - }); - - afterAll(() => { resetConfigAndMocks(paths.fuelsConfigPath); resetDiskAndMocks(paths.root); paths = bootstrapProject(__filename); From 3ecfed6bb165775484ce144499594dc1c3ebfe1a Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Thu, 3 Oct 2024 15:27:32 +0530 Subject: [PATCH 12/14] fix tests --- packages/fuels/test/features/deploy.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuels/test/features/deploy.test.ts b/packages/fuels/test/features/deploy.test.ts index 8dc0f57b03f..7d36998bf3d 100644 --- a/packages/fuels/test/features/deploy.test.ts +++ b/packages/fuels/test/features/deploy.test.ts @@ -22,10 +22,10 @@ describe('deploy', { timeout: 180000 }, () => { let paths = bootstrapProject(__filename); beforeEach(() => { - mockCheckForUpdates(); resetConfigAndMocks(paths.fuelsConfigPath); resetDiskAndMocks(paths.root); paths = bootstrapProject(__filename); + mockCheckForUpdates(); }); afterEach(() => { From 9396b0ecb71b3ba5e26372cd7043c277f60ff1e8 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 4 Oct 2024 16:08:35 +0530 Subject: [PATCH 13/14] refactor --- packages/fuels/src/cli/utils/fuelsVersionCache.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/fuels/src/cli/utils/fuelsVersionCache.ts b/packages/fuels/src/cli/utils/fuelsVersionCache.ts index c69fa336801..093573e99b5 100644 --- a/packages/fuels/src/cli/utils/fuelsVersionCache.ts +++ b/packages/fuels/src/cli/utils/fuelsVersionCache.ts @@ -12,12 +12,19 @@ export const saveToCache = (cache: FuelsVersionCache) => { export const FUELS_VERSION_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours in milliseconds export const checkAndLoadCache = (): FuelsVersionCache | null => { - if (fs.existsSync(FUELS_VERSION_CACHE_FILE)) { + const doesVersionCacheExist = fs.existsSync(FUELS_VERSION_CACHE_FILE); + + if (doesVersionCacheExist) { const cachedVersion = fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8'); - const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE); - if (cachedVersion && Date.now() - cacheTimestamp < FUELS_VERSION_CACHE_TTL) { - return cachedVersion; + if (!cachedVersion) { + return null; } + + const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE); + const hasCacheExpired = Date.now() - cacheTimestamp < FUELS_VERSION_CACHE_TTL; + + return hasCacheExpired ? null : cachedVersion; } + return null; }; From a8f0431351e2fc94df6e4977d8cb8dc473e93229 Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Fri, 4 Oct 2024 16:11:10 +0530 Subject: [PATCH 14/14] use helper --- packages/fuels/src/cli.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/fuels/src/cli.test.ts b/packages/fuels/src/cli.test.ts index 5a230ec449e..257f33886d6 100644 --- a/packages/fuels/src/cli.test.ts +++ b/packages/fuels/src/cli.test.ts @@ -1,8 +1,9 @@ import { Command } from 'commander'; +import { mockCheckForUpdates } from '../test/utils/mockCheckForUpdates'; + import * as cliMod from './cli'; import { Commands } from './cli/types'; -import * as checkForUpdatesMod from './cli/utils/checkForAndDisplayUpdates'; import * as loggingMod from './cli/utils/logger'; import { run } from './run'; @@ -78,9 +79,7 @@ describe('cli.js', () => { .mockReturnValue(Promise.resolve(command)); const configureCli = vi.spyOn(cliMod, 'configureCli').mockImplementation(() => new Command()); - vi.spyOn(checkForUpdatesMod, 'checkForAndDisplayUpdates').mockImplementation(() => - Promise.resolve() - ); + mockCheckForUpdates(); await run([]);