Implemented three new API endpoints for knowledge graph visualization: 1. GET /api/knowledge/graph - Full knowledge graph - Returns all entries and links with optional filtering - Supports filtering by tags, status, and node count limit - Includes orphan detection (entries with no links) 2. GET /api/knowledge/graph/stats - Graph statistics - Total entries and links counts - Orphan entries detection - Average links per entry - Top 10 most connected entries - Tag distribution across entries 3. GET /api/knowledge/graph/:slug - Entry-centered subgraph - Returns graph centered on specific entry - Supports depth parameter (1-5) for traversal distance - Includes all connected nodes up to specified depth New Files: - apps/api/src/knowledge/graph.controller.ts - apps/api/src/knowledge/graph.controller.spec.ts Modified Files: - apps/api/src/knowledge/dto/graph-query.dto.ts (added GraphFilterDto) - apps/api/src/knowledge/entities/graph.entity.ts (extended with new types) - apps/api/src/knowledge/services/graph.service.ts (added new methods) - apps/api/src/knowledge/services/graph.service.spec.ts (added tests) - apps/api/src/knowledge/knowledge.module.ts (registered controller) - apps/api/src/knowledge/dto/index.ts (exported new DTOs) - docs/scratchpads/71-graph-data-api.md (implementation notes) Test Coverage: 21 tests (all passing) - 14 service tests including orphan detection, filtering, statistics - 7 controller tests for all three endpoints Follows TDD principles with tests written before implementation. All code quality gates passed (lint, typecheck, tests). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
230 lines
7.0 KiB
TypeScript
230 lines
7.0 KiB
TypeScript
import { ConfigService } from "@nestjs/config";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { GitOperationsService } from "./git-operations.service";
|
|
import { GitOperationError } from "./types";
|
|
|
|
// Mock simple-git
|
|
const mockGit = {
|
|
clone: vi.fn(),
|
|
checkoutLocalBranch: vi.fn(),
|
|
add: vi.fn(),
|
|
commit: vi.fn(),
|
|
push: vi.fn(),
|
|
addConfig: vi.fn(),
|
|
};
|
|
|
|
vi.mock("simple-git", () => ({
|
|
simpleGit: vi.fn(() => mockGit),
|
|
}));
|
|
|
|
describe("GitOperationsService", () => {
|
|
let service: GitOperationsService;
|
|
let mockConfigService: ConfigService;
|
|
|
|
beforeEach(() => {
|
|
// Reset all mocks
|
|
vi.clearAllMocks();
|
|
|
|
// Create mock config service
|
|
mockConfigService = {
|
|
get: vi.fn((key: string) => {
|
|
if (key === "orchestrator.git.userName") return "Test User";
|
|
if (key === "orchestrator.git.userEmail") return "test@example.com";
|
|
return undefined;
|
|
}),
|
|
} as any;
|
|
|
|
// Create service with mock
|
|
service = new GitOperationsService(mockConfigService);
|
|
});
|
|
|
|
describe("cloneRepository", () => {
|
|
it("should clone a repository successfully", async () => {
|
|
mockGit.clone.mockResolvedValue(undefined);
|
|
|
|
await service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo");
|
|
|
|
expect(mockGit.clone).toHaveBeenCalledWith(
|
|
"https://github.com/test/repo.git",
|
|
"/tmp/repo",
|
|
);
|
|
});
|
|
|
|
it("should clone a repository with specific branch", async () => {
|
|
mockGit.clone.mockResolvedValue(undefined);
|
|
|
|
await service.cloneRepository(
|
|
"https://github.com/test/repo.git",
|
|
"/tmp/repo",
|
|
"develop",
|
|
);
|
|
|
|
expect(mockGit.clone).toHaveBeenCalledWith(
|
|
"https://github.com/test/repo.git",
|
|
"/tmp/repo",
|
|
["--branch", "develop"],
|
|
);
|
|
});
|
|
|
|
it("should throw GitOperationError on clone failure", async () => {
|
|
const error = new Error("Clone failed");
|
|
mockGit.clone.mockRejectedValue(error);
|
|
|
|
await expect(
|
|
service.cloneRepository("https://github.com/test/repo.git", "/tmp/repo"),
|
|
).rejects.toThrow(GitOperationError);
|
|
|
|
try {
|
|
await service.cloneRepository(
|
|
"https://github.com/test/repo.git",
|
|
"/tmp/repo",
|
|
);
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(GitOperationError);
|
|
expect((e as GitOperationError).operation).toBe("clone");
|
|
expect((e as GitOperationError).cause).toBe(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("createBranch", () => {
|
|
it("should create and checkout a new branch", async () => {
|
|
mockGit.checkoutLocalBranch.mockResolvedValue(undefined);
|
|
|
|
await service.createBranch("/tmp/repo", "feature/new-branch");
|
|
|
|
expect(mockGit.checkoutLocalBranch).toHaveBeenCalledWith(
|
|
"feature/new-branch",
|
|
);
|
|
});
|
|
|
|
it("should throw GitOperationError on branch creation failure", async () => {
|
|
const error = new Error("Branch already exists");
|
|
mockGit.checkoutLocalBranch.mockRejectedValue(error);
|
|
|
|
await expect(
|
|
service.createBranch("/tmp/repo", "feature/new-branch"),
|
|
).rejects.toThrow(GitOperationError);
|
|
|
|
try {
|
|
await service.createBranch("/tmp/repo", "feature/new-branch");
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(GitOperationError);
|
|
expect((e as GitOperationError).operation).toBe("createBranch");
|
|
expect((e as GitOperationError).cause).toBe(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("commit", () => {
|
|
it("should stage all changes and commit with message", async () => {
|
|
mockGit.add.mockResolvedValue(undefined);
|
|
mockGit.commit.mockResolvedValue({ commit: "abc123" });
|
|
|
|
await service.commit("/tmp/repo", "feat: add new feature");
|
|
|
|
expect(mockGit.add).toHaveBeenCalledWith(".");
|
|
expect(mockGit.commit).toHaveBeenCalledWith("feat: add new feature");
|
|
});
|
|
|
|
it("should stage specific files when provided", async () => {
|
|
mockGit.add.mockResolvedValue(undefined);
|
|
mockGit.commit.mockResolvedValue({ commit: "abc123" });
|
|
|
|
await service.commit("/tmp/repo", "fix: update files", [
|
|
"file1.ts",
|
|
"file2.ts",
|
|
]);
|
|
|
|
expect(mockGit.add).toHaveBeenCalledWith(["file1.ts", "file2.ts"]);
|
|
expect(mockGit.commit).toHaveBeenCalledWith("fix: update files");
|
|
});
|
|
|
|
it("should configure git user before committing", async () => {
|
|
mockGit.add.mockResolvedValue(undefined);
|
|
mockGit.commit.mockResolvedValue({ commit: "abc123" });
|
|
mockGit.addConfig.mockResolvedValue(undefined);
|
|
|
|
await service.commit("/tmp/repo", "test commit");
|
|
|
|
expect(mockGit.addConfig).toHaveBeenCalledWith("user.name", "Test User");
|
|
expect(mockGit.addConfig).toHaveBeenCalledWith(
|
|
"user.email",
|
|
"test@example.com",
|
|
);
|
|
});
|
|
|
|
it("should throw GitOperationError on commit failure", async () => {
|
|
mockGit.add.mockResolvedValue(undefined);
|
|
const error = new Error("Nothing to commit");
|
|
mockGit.commit.mockRejectedValue(error);
|
|
|
|
await expect(service.commit("/tmp/repo", "test commit")).rejects.toThrow(
|
|
GitOperationError,
|
|
);
|
|
|
|
try {
|
|
await service.commit("/tmp/repo", "test commit");
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(GitOperationError);
|
|
expect((e as GitOperationError).operation).toBe("commit");
|
|
expect((e as GitOperationError).cause).toBe(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("push", () => {
|
|
it("should push to origin and current branch by default", async () => {
|
|
mockGit.push.mockResolvedValue(undefined);
|
|
|
|
await service.push("/tmp/repo");
|
|
|
|
expect(mockGit.push).toHaveBeenCalledWith("origin", undefined);
|
|
});
|
|
|
|
it("should push to specified remote and branch", async () => {
|
|
mockGit.push.mockResolvedValue(undefined);
|
|
|
|
await service.push("/tmp/repo", "upstream", "main");
|
|
|
|
expect(mockGit.push).toHaveBeenCalledWith("upstream", "main");
|
|
});
|
|
|
|
it("should support force push", async () => {
|
|
mockGit.push.mockResolvedValue(undefined);
|
|
|
|
await service.push("/tmp/repo", "origin", "develop", true);
|
|
|
|
expect(mockGit.push).toHaveBeenCalledWith("origin", "develop", {
|
|
"--force": null,
|
|
});
|
|
});
|
|
|
|
it("should throw GitOperationError on push failure", async () => {
|
|
const error = new Error("Push rejected");
|
|
mockGit.push.mockRejectedValue(error);
|
|
|
|
await expect(service.push("/tmp/repo")).rejects.toThrow(GitOperationError);
|
|
|
|
try {
|
|
await service.push("/tmp/repo");
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(GitOperationError);
|
|
expect((e as GitOperationError).operation).toBe("push");
|
|
expect((e as GitOperationError).cause).toBe(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("git config", () => {
|
|
it("should read git config from ConfigService", () => {
|
|
expect(mockConfigService.get("orchestrator.git.userName")).toBe(
|
|
"Test User",
|
|
);
|
|
expect(mockConfigService.get("orchestrator.git.userEmail")).toBe(
|
|
"test@example.com",
|
|
);
|
|
});
|
|
});
|
|
});
|