Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### Patch Changes

- A very fast and simple background version check

## 0.0.19

### Patch Changes

- ensure the cli works on windows

## 0.0.18
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build": "tsc --noEmit && tsc",
"build:ci": "tsc",
"clean": "rimraf dist",
"clean:all": "rimraf dist node_modules",
"lint": "eslint \"src/**/*.ts\" --fix",
"format": "prettier --write \"src/**/*.*\"",
"test": "vitest run",
Expand Down Expand Up @@ -51,6 +52,7 @@
"@anthropic-ai/sdk": "^0.36",
"chalk": "^5",
"dotenv": "^16",
"semver": "^7.7.1",
"source-map-support": "^0.5",
"uuid": "^11",
"yargs": "^17",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from "fs";
import * as os from "os";
import * as path from "path";

const settingsDir = path.join(os.homedir(), ".mycoder");

export const getSettingsDir = (): string => {
if (!fs.existsSync(settingsDir)) {
fs.mkdirSync(settingsDir, { recursive: true });
}
return settingsDir;
};
18 changes: 9 additions & 9 deletions src/tools/system/shellStart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("shellStartTool", () => {
description: "Test process",
timeout: 500, // Generous timeout to ensure sync mode
},
{ logger },
{ logger }
);

expect(result.mode).toBe("sync");
Expand All @@ -41,7 +41,7 @@ describe("shellStartTool", () => {
description: "Slow command test",
timeout: 50, // Short timeout to force async mode
},
{ logger },
{ logger }
);

expect(result.mode).toBe("async");
Expand All @@ -57,7 +57,7 @@ describe("shellStartTool", () => {
command: "nonexistentcommand",
description: "Invalid command test",
},
{ logger },
{ logger }
);

expect(result.mode).toBe("sync");
Expand All @@ -75,7 +75,7 @@ describe("shellStartTool", () => {
description: "Sync completion test",
timeout: 500,
},
{ logger },
{ logger }
);

// Even sync results should be in processStates
Expand All @@ -88,7 +88,7 @@ describe("shellStartTool", () => {
description: "Async completion test",
timeout: 50,
},
{ logger },
{ logger }
);

if (asyncResult.mode === "async") {
Expand All @@ -103,7 +103,7 @@ describe("shellStartTool", () => {
description: "Pipe test",
timeout: 50, // Force async for interactive command
},
{ logger },
{ logger }
);

expect(result.mode).toBe("async");
Expand All @@ -130,15 +130,15 @@ describe("shellStartTool", () => {
}
});

it("should use default timeout of 100ms", async () => {
it("should use default timeout of 10000ms", async () => {
const result = await shellStartTool.execute(
{
command: "sleep 1",
description: "Default timeout test",
},
{ logger },
{ logger }
);

expect(result.mode).toBe("async");
expect(result.mode).toBe("sync");
});
});
6 changes: 4 additions & 2 deletions src/tools/system/shellStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const parameterSchema = z.object({
timeout: z
.number()
.optional()
.describe("Timeout in ms before switching to async mode (default: 100ms)"),
.describe(
"Timeout in ms before switching to async mode (default: 10s, which usually is sufficient)"
),
});

const returnSchema = z.union([
Expand Down Expand Up @@ -58,7 +60,7 @@ const returnSchema = z.union([
type Parameters = z.infer<typeof parameterSchema>;
type ReturnType = z.infer<typeof returnSchema>;

const DEFAULT_TIMEOUT = 100;
const DEFAULT_TIMEOUT = 1000 * 10;

export const shellStartTool: Tool<Parameters, ReturnType> = {
name: "shellStart",
Expand Down
90 changes: 79 additions & 11 deletions src/utils/versionCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/* eslint-disable max-lines-per-function */
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import {
generateUpgradeMessage,
fetchLatestVersion,
getPackageInfo,
checkForUpdates,
} from "./versionCheck.js";
import * as fs from "fs";
import * as fsPromises from "fs/promises";
import * as path from "path";
import { getSettingsDir } from "../settings/settings.js";

vi.mock("fs");
vi.mock("fs/promises");
vi.mock("../settings/settings.js");

describe("versionCheck", () => {
describe("generateUpgradeMessage", () => {
it("returns null when versions are the same", () => {
expect(generateUpgradeMessage("1.0.0", "1.0.0", "test-package")).toBe(
null,
null
);
});

Expand All @@ -19,6 +28,12 @@ describe("versionCheck", () => {
expect(message).toContain("Update available: 1.0.0 → 1.1.0");
expect(message).toContain("Run 'npm install -g test-package' to update");
});

it("returns null when current version is higher", () => {
expect(generateUpgradeMessage("2.0.0", "1.0.0", "test-package")).toBe(
null
);
});
});

describe("fetchLatestVersion", () => {
Expand All @@ -43,18 +58,30 @@ describe("versionCheck", () => {
const version = await fetchLatestVersion("test-package");
expect(version).toBe("1.1.0");
expect(mockFetch).toHaveBeenCalledWith(
"https://registry.npmjs.org/test-package/latest",
"https://registry.npmjs.org/test-package/latest"
);
});

it("returns null when fetch fails", async () => {
it("throws error when fetch fails", async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
statusText: "Not Found",
});

const version = await fetchLatestVersion("test-package");
expect(version).toBe(null);
await expect(fetchLatestVersion("test-package")).rejects.toThrow(
"Failed to fetch version info: Not Found"
);
});

it("throws error when version is missing from response", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({}),
});

await expect(fetchLatestVersion("test-package")).rejects.toThrow(
"Version info not found in response"
);
});
});

Expand All @@ -71,28 +98,69 @@ describe("versionCheck", () => {
describe("checkForUpdates", () => {
const mockFetch = vi.fn();
const originalFetch = global.fetch;
const originalEnv = process.env;
const mockSettingsDir = "/mock/settings/dir";
const versionFilePath = path.join(mockSettingsDir, "lastVersionCheck");

beforeEach(() => {
global.fetch = mockFetch;
process.env = { ...originalEnv };
vi.mocked(getSettingsDir).mockReturnValue(mockSettingsDir);
vi.mocked(fs.existsSync).mockReturnValue(false);
});

afterEach(() => {
global.fetch = originalFetch;
process.env = originalEnv;
vi.clearAllMocks();
});

it("returns upgrade message when update available", async () => {
process.env.npm_config_global = "true";
it("returns null and initiates background check when no cached version", async () => {
vi.mocked(fs.existsSync).mockReturnValue(false);
mockFetch.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({ version: "999.0.0" }), // Much higher version
json: () => Promise.resolve({ version: "2.0.0" }),
});

const result = await checkForUpdates();
expect(result).toBe(null);

// Wait for setImmediate to complete
await new Promise((resolve) => setImmediate(resolve));

expect(mockFetch).toHaveBeenCalled();
expect(fsPromises.writeFile).toHaveBeenCalledWith(
versionFilePath,
"2.0.0",
"utf8"
);
});

it("returns upgrade message when cached version is newer", async () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fsPromises.readFile).mockResolvedValue("2.0.0");

const result = await checkForUpdates();
expect(result).toContain("Update available");
});

it("handles errors gracefully during version check", async () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fsPromises.readFile).mockRejectedValue(new Error("Test error"));

const result = await checkForUpdates();
expect(result).toBe(null);
});

it("handles errors gracefully during background update", async () => {
vi.mocked(fs.existsSync).mockReturnValue(false);
mockFetch.mockRejectedValue(new Error("Network error"));

const result = await checkForUpdates();
expect(result).toBe(null);

// Wait for setImmediate to complete
await new Promise((resolve) => setImmediate(resolve));

// Verify the error was handled
expect(fsPromises.writeFile).not.toHaveBeenCalled();
});
});
});
Loading