1
-import { describe, expect, it, vi } from '@effect/vitest';
2
-import { ConfigProvider, Effect, Layer, Option } from 'effect';
1
+import { afterAll, afterEach, describe, expect, it, vi } from '@effect/vitest';
2
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3
+import { tmpdir } from 'node:os';
4
+import { join } from 'node:path';
5
+import { ConfigProvider, Effect, Exit, Layer, Option } from 'effect';
6
import { FetchHttpClient } from 'effect/http';
7
import * as FileSystem from 'effect/FileSystem';
8
import * as Path from 'effect/Path';
58
})
59
);
60
61
+// A private home directory: stable-channel lookups record the update-check cache under it.
62
+const testHomedir = mkdtempSync(join(tmpdir(), 'composio-upgrade-home-'));
63
+const updateCheckDir = join(testHomedir, '.composio');
64
+const updateCheckFile = join(updateCheckDir, 'update-check.json');
65
+afterEach(() => rmSync(updateCheckDir, { recursive: true, force: true }));
66
+afterAll(() => rmSync(testHomedir, { recursive: true, force: true }));
67
+
68
const NodeOsTest = Layer.succeed(
69
NodeOs,
70
NodeOs.of({
61
- homedir: '/tmp',
71
+ homedir: testHomedir,
72
tmpdir: '/tmp',
73
platform: 'darwin',
74
arch: 'arm64',
698
});
699
});
700
701
+describe('UpgradeBinary update-check cache', () => {
702
+ // The cache that misled the user: written before 0.5.0 was released.
703
+ const staleState = { lastChecked: '2026-10-21T14:31:56.646Z', latestVersion: '0.4.5' };
704
+
705
+ const writeStaleState = () => {
706
+ mkdirSync(updateCheckDir, { recursive: true });
707
+ writeFileSync(updateCheckFile, JSON.stringify(staleState));
708
+ };
709
+
710
+ const readCachedState = (): unknown => JSON.parse(readFileSync(updateCheckFile, 'utf8'));
711
+
712
+ const release = (version: string) => ({
713
+ tag_name: `@composio/cli@${version}`,
714
+ draft: false,
715
+ prerelease: version.includes('-'),
716
+ assets: [
717
+ {
718
+ name: 'composio-darwin-aarch64.zip',
719
+ browser_download_url: `http://127.0.0.1/${version}.zip`,
720
+ },
721
+ ],
722
+ });
723
+
724
+ /** Serves the release list, plus any single release by tag, and installs `currentTag`. */
725
+ const setUpReleases = (currentTag: string) =>
726
+ Effect.gen(function* () {
727
+ const fs = yield* FileSystem.FileSystem;
728
+ const path = yield* Path.Path;
729
+ const installDir = yield* fs.makeTempDirectoryScoped({ prefix: 'composio-upgrade-cache-' });
730
+ yield* fs.writeFileString(path.join(installDir, 'release-tag.txt'), `${currentTag}\n`);
731
+ vi.spyOn(process, 'execPath', 'get').mockReturnValue(path.join(installDir, 'composio'));
732
+
733
+ const apiBaseUrl = yield* scopedHttpServer((req, res) => {
734
+ res.writeHead(200, { 'content-type': 'application/json' });
735
+ const taggedPrefix = '/repos/test-owner/test-repo/releases/tags/';
736
+ res.end(
737
+ JSON.stringify(
738
+ req.url?.startsWith(taggedPrefix)
739
+ ? release(decodeURIComponent(req.url.slice(taggedPrefix.length)).split('@').at(-1)!)
740
+ : [release('0.5.1-beta.1'), release('0.5.0'), release('0.4.5'), release('0.4.2')]
741
+ )
742
+ );
743
+ });
744
+
745
+ return [
746
+ ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl],
747
+ ['COMPOSIO_GITHUB_OWNER', 'test-owner'],
748
+ ['COMPOSIO_GITHUB_REPO', 'test-repo'],
749
+ ] as Array<[string, string]>;
750
+ });
751
+
752
+ it.effect('records the live-resolved latest stable release', () => {
753
+ vi.stubGlobal('Bun', { which: vi.fn(() => null) });
754
+ // Only Date is faked: the upgrade awaits a real local HTTP server.
755
+ vi.useFakeTimers({ toFake: ['Date'] });
756
+ vi.setSystemTime(new Date('2026-10-21T16:20:00.000Z'));
757
+
758
+ return Effect.gen(function* () {
759
+ writeStaleState();
760
+ const config = yield* setUpReleases('@composio/cli@0.5.0');
761
+
762
+ const result = yield* runUpgradeSuccess(config);
763
+
764
+ expect(result).toBeUndefined();
765
+ expect(readCachedState()).toEqual({
766
+ lastChecked: '2026-10-21T16:20:00.000Z',
767
+ latestVersion: '0.5.0',
768
+ });
769
+ }).pipe(
770
+ Effect.provide(TestPlatform),
771
+ Effect.ensuring(Effect.sync(() => vi.useRealTimers())),
772
+ Effect.ensuring(restoreStubsAndMocks)
773
+ );
774
+ });
775
+
776
+ it.effect.each([
777
+ { target: 'a version argument', options: { tag: '@composio/cli@0.4.2' }, env: [] },
778
+ {
779
+ target: 'COMPOSIO_GITHUB_TAG',
780
+ options: {},
781
+ env: [['COMPOSIO_GITHUB_TAG', '@composio/cli@0.4.2']] as Array<[string, string]>,
782
+ },
783
+ { target: '--beta', options: { prerelease: true }, env: [] },
784
+ ])('leaves the cache alone when $target picks the target', ({ options, env }) => {
785
+ vi.stubGlobal('Bun', { which: vi.fn(() => null) });
786
+
787
+ return Effect.gen(function* () {
788
+ writeStaleState();
789
+ const config = yield* setUpReleases('@composio/cli@0.4.0');
790
+
791
+ // The download is unreachable, so only the release lookup could have touched the cache.
792
+ const exit = yield* Effect.exit(makeUpgradeEffect([...config, ...env], options));
793
+
794
+ expect(Exit.isFailure(exit)).toBe(true);
795
+ expect(readCachedState()).toEqual(staleState);
796
+ }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
797
+ });
798
+
799
+ it.effect('still upgrades when the cache cannot be written', () => {
800
+ vi.stubGlobal('Bun', { which: vi.fn(() => null) });
801
+
802
+ return Effect.gen(function* () {
803
+ // A file where the cache directory should be makes every cache write fail.
804
+ writeFileSync(updateCheckDir, 'not a directory');
805
+ const config = yield* setUpReleases('@composio/cli@0.5.0');
806
+
807
+ const result = yield* runUpgradeSuccess(config);
808
+
809
+ expect(result).toBeUndefined();
810
+ expect(readFileSync(updateCheckDir, 'utf8')).toBe('not a directory');
811
+ }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
812
+ });
813
+});
814
+
815
describe('formatDownloadProgress', () => {
816
it('reports percent and both sizes when the total is known', () => {
817
expect(formatDownloadProgress({ receivedBytes: 142_000_000, totalBytes: 338_000_000 })).toBe(