change to 'gitDiffCache' and add tests

This commit is contained in:
Dallin Romney 2025-05-28 15:31:18 -07:00
parent f32f78c550
commit 34166b6af9
7 changed files with 97 additions and 11 deletions

View file

@ -3,7 +3,7 @@ import { findUriInDirs } from "../../util/uri";
import { ContextRetrievalService } from "../context/ContextRetrievalService";
import { GetLspDefinitionsFunction } from "../types";
import { HelperVars } from "../util/HelperVars";
import { getDiffsFromCache } from "./diffSnippetCache";
import { getDiffsFromCache } from "./gitDiffCache";
import {
AutocompleteClipboardSnippet,

View file

@ -0,0 +1,84 @@
import { GitDiffCache } from "./gitDiffCache";
beforeEach(() => {
// Add this line to GitDiffCache class to make instance accessible
(GitDiffCache as any).instance = null;
});
test("GitDiffCache returns cached results within cache time", async () => {
const mockDiff = ["file1.ts", "file2.ts"];
const getDiffFn = jest.fn().mockResolvedValue(mockDiff);
const cache = GitDiffCache.getInstance(getDiffFn, 1); // 1 second cache
const result1 = await cache.get();
const result2 = await cache.get();
expect(result1).toEqual(mockDiff);
expect(result2).toEqual(mockDiff);
expect(getDiffFn).toHaveBeenCalledTimes(1);
});
test("GitDiffCache refreshes cache after expiration", async () => {
const mockDiff = ["file1.ts"];
const getDiffFn = jest.fn().mockResolvedValue(mockDiff);
const cache = GitDiffCache.getInstance(getDiffFn, 0.1); // 100ms cache
const result1 = await cache.get();
await new Promise((resolve) => setTimeout(resolve, 200)); // Wait for cache to expire
const result2 = await cache.get();
expect(getDiffFn).toHaveBeenCalledTimes(2);
});
test("GitDiffCache returns empty array on error", async () => {
const getDiffFn = jest.fn().mockRejectedValue(new Error("Git error"));
const cache = GitDiffCache.getInstance(getDiffFn);
const result = await cache.get();
expect(result).toEqual([]);
});
test("GitDiffCache reuses pending request", async () => {
const mockDiff = ["file1.ts"];
let resolvePromise: (value: string[]) => void;
const getDiffFn = jest.fn().mockImplementation(() => {
return new Promise((resolve) => {
resolvePromise = resolve;
});
});
const cache = GitDiffCache.getInstance(getDiffFn);
const promise1 = cache.get();
const promise2 = cache.get();
resolvePromise!(mockDiff);
const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toEqual(mockDiff);
expect(result2).toEqual(mockDiff);
expect(getDiffFn).toHaveBeenCalledTimes(1);
});
test("GitDiffCache invalidate clears cache", async () => {
const mockDiff = ["file1.ts"];
const getDiffFn = jest.fn().mockResolvedValue(mockDiff);
const cache = GitDiffCache.getInstance(getDiffFn);
await cache.get();
cache.invalidate();
await cache.get();
expect(getDiffFn).toHaveBeenCalledTimes(2);
});
test("GitDiffCache maintains singleton instance", () => {
const getDiffFn1 = jest.fn();
const getDiffFn2 = jest.fn();
const instance1 = GitDiffCache.getInstance(getDiffFn1);
const instance2 = GitDiffCache.getInstance(getDiffFn2);
expect(instance1).toBe(instance2);
});

View file

@ -31,6 +31,9 @@ export class GitDiffCache {
this.cachedDiff = diff;
this.lastFetchTime = Date.now();
return this.cachedDiff;
} catch (e) {
console.error("Error fetching git diff:", e);
return [];
} finally {
this.pendingRequest = null;
}

View file

@ -45,10 +45,7 @@ import {
} from ".";
import { ConfigYaml } from "@continuedev/config-yaml";
import {
getDiffFn,
GitDiffCache,
} from "./autocomplete/snippets/diffSnippetCache";
import { getDiffFn, GitDiffCache } from "./autocomplete/snippets/gitDiffCache";
import { isLocalAssistantFile } from "./config/loadLocalAssistants";
import {
setupBestConfig,

View file

@ -1,5 +1,5 @@
import { ContinueSDK, SlashCommand } from "../..";
import { getDiffsFromCache } from "../../autocomplete/snippets/diffSnippetCache";
import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache";
import { renderChatMessage } from "../../util/messageContent";
import { getLastNPathParts } from "../../util/uri";
import { parsePromptFileV1V2 } from "../v2/parsePromptFileV1V2";

View file

@ -1,5 +1,5 @@
import { ToolImpl } from ".";
import { getDiffsFromCache } from "../../autocomplete/snippets/diffSnippetCache";
import { getDiffsFromCache } from "../../autocomplete/snippets/gitDiffCache";
export const viewDiffImpl: ToolImpl = async (args, extras) => {
const diffs = await getDiffsFromCache(extras.ide); // const diffs = await extras.ide.getDiff(true);

View file

@ -29,8 +29,10 @@ class Test {
result = 0.0
return this
}
}