Skip to content

Commit 4011449

Browse files
Copilotfuxingloh
andcommitted
feat: allow disabling auto-update via config env.USE_AGENTLY_AUTO_UPDATE
Co-authored-by: fuxingloh <4266087+fuxingloh@users.noreply.github.com>
1 parent fd6b1dd commit 4011449

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

packages/use-agently/src/commands/update.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ import { captureOutput } from "../testing";
33

44
const mockLoadState = mock(async () => ({}));
55
const mockSaveState = mock(async (_state: unknown) => {});
6+
const mockLoadConfig = mock(async () => undefined as unknown);
67

78
mock.module("../state", () => ({
89
loadState: mockLoadState,
910
saveState: mockSaveState,
1011
}));
1112

13+
mock.module("../config", () => ({
14+
loadConfig: mockLoadConfig,
15+
saveConfig: async () => {},
16+
backupConfig: async () => "",
17+
getConfigOrThrow: async () => {
18+
throw new Error("No wallet configured.");
19+
},
20+
}));
21+
1222
mock.module("node:child_process", () => ({
1323
execSync: mock(() => {}),
1424
}));
@@ -121,6 +131,7 @@ describe("checkAutoUpdate", () => {
121131
beforeEach(() => {
122132
mockLoadState.mockClear();
123133
mockSaveState.mockClear();
134+
mockLoadConfig.mockImplementation(async () => undefined);
124135
fetchSpy = spyOn(globalThis, "fetch").mockResolvedValue({
125136
ok: true,
126137
json: async () => ({ version: "9.9.9" }),
@@ -158,6 +169,23 @@ describe("checkAutoUpdate", () => {
158169
expect(mockSaveState).toHaveBeenCalledTimes(1);
159170
});
160171

172+
test("skips when USE_AGENTLY_AUTO_UPDATE is 0 in config", async () => {
173+
mockLoadConfig.mockImplementation(async () => ({ env: { USE_AGENTLY_AUTO_UPDATE: 0 } }));
174+
175+
await checkAutoUpdate();
176+
177+
expect(mockSaveState).not.toHaveBeenCalled();
178+
expect(fetchSpy).not.toHaveBeenCalled();
179+
});
180+
181+
test("runs when USE_AGENTLY_AUTO_UPDATE is 1 in config", async () => {
182+
mockLoadConfig.mockImplementation(async () => ({ env: { USE_AGENTLY_AUTO_UPDATE: 1 } }));
183+
184+
await checkAutoUpdate();
185+
186+
expect(mockSaveState).toHaveBeenCalledTimes(1);
187+
});
188+
161189
test("silently swallows errors", async () => {
162190
fetchSpy.mockResolvedValue({ ok: false, status: 500 } as Response);
163191
mockLoadState.mockImplementation(async () => ({}));

packages/use-agently/src/commands/update.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Command } from "commander";
22
import { execSync } from "node:child_process";
33
import { output } from "../output.js";
44
import { loadState, saveState } from "../state.js";
5+
import { loadConfig } from "../config.js";
56
import pkg from "../../package.json" with { type: "json" };
67

78
const PACKAGE_NAME = "use-agently";
@@ -50,6 +51,9 @@ export async function checkAndUpdate(): Promise<{ current: string; latest: strin
5051

5152
export async function checkAutoUpdate(): Promise<void> {
5253
try {
54+
const config = await loadConfig();
55+
if ((config?.env?.USE_AGENTLY_AUTO_UPDATE ?? 1) === 0) return;
56+
5357
const state = await loadState();
5458
const lastCheck = state.lastUpdateCheck ? new Date(state.lastUpdateCheck).getTime() : 0;
5559
const hoursSinceLastCheck = (Date.now() - lastCheck) / (1000 * 60 * 60);

packages/use-agently/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface WalletConfig {
1111

1212
export interface Config {
1313
wallet: WalletConfig;
14+
env?: Record<string, number | string>;
1415
}
1516

1617
function getConfigDir(scope: ConfigScope): string {

0 commit comments

Comments
 (0)