change to 'gitDiffCache' and add tests
This commit is contained in:
parent
f32f78c550
commit
34166b6af9
7 changed files with 97 additions and 11 deletions
|
|
@ -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,
|
||||
|
|
|
|||
84
core/autocomplete/snippets/gitDiffCache.test.ts
Normal file
84
core/autocomplete/snippets/gitDiffCache.test.ts
Normal 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);
|
||||
});
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -29,8 +29,10 @@ class Test {
|
|||
result = 0.0
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue