import { describe, it, expect, vi } from "vitest";
import { analyzeGoModHandler, goStructHelperHandler } from "./go.js";
import fs from "fs/promises";
vi.mock("fs/promises");
describe("Go Tools", () => {
describe("analyzeGoModHandler", () => {
it("should parse valid go.mod", async () => {
(fs.readFile as any).mockResolvedValue(`
module github.com/user/project
go 1.21
require (
github.com/gin-gonic/gin v1.9.1
github.com/stretchr/testify v1.8.4 // indirect
)
`);
const result = await analyzeGoModHandler({ filePath: "go.mod" });
expect(result.content[0].text).toContain("github.com/user/project");
expect(result.content[0].text).toContain("1.21");
expect(result.content[0].text).toContain("github.com/gin-gonic/gin");
});
it("should handle errors gracefully", async () => {
(fs.readFile as any).mockRejectedValue(new Error("File not found"));
const result = await analyzeGoModHandler({ filePath: "go.mod" });
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain("Error reading go.mod");
});
});
describe("goStructHelperHandler", () => {
it("should generate simple struct", async () => {
const result = await goStructHelperHandler({
name: "User",
fields: [
{ name: "ID", type: "int", jsonTag: "id" },
{ name: "Name", type: "string", jsonTag: "name" },
],
});
expect(result.content[0].text).toContain("type User struct");
expect(result.content[0].text).toContain('ID int `json:"id"`');
expect(result.content[0].text).toContain('Name string `json:"name"`');
});
});
});