From ae636520ca5ce39ed8741a1d2a8847804f429d4a Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 29 Oct 2024 22:23:59 -0700 Subject: [PATCH] reverse and test --- core/indexing/refreshIndex.test.ts | 34 ++++++++++++++++++++++++++++++ core/indexing/refreshIndex.ts | 16 +++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 core/indexing/refreshIndex.test.ts diff --git a/core/indexing/refreshIndex.test.ts b/core/indexing/refreshIndex.test.ts new file mode 100644 index 000000000..7eeb2c5d8 --- /dev/null +++ b/core/indexing/refreshIndex.test.ts @@ -0,0 +1,34 @@ +import { truncateToLastNBytes } from "./refreshIndex"; + +describe("truncateToLastNBytes", () => { + it("should return full string if maxBytes greater than string byte length", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 100); + expect(result).toBe("Hello World"); + }); + + it("should truncate ASCII string correctly", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("World"); + }); + + it("should handle empty string", () => { + const input = ""; + const result = truncateToLastNBytes(input, 5); + expect(result).toBe(""); + }); + + it("should handle UTF-8 characters correctly", () => { + const input = "👋 Hello"; + // 👋 is 4 bytes, space is 1 byte + const result = truncateToLastNBytes(input, 5); + expect(result).toBe("Hello"); + }); + + it("should handle maxBytes of 0", () => { + const input = "Hello World"; + const result = truncateToLastNBytes(input, 0); + expect(result).toBe(""); + }); +}); diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index a2072c60e..ba2dbd820 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -406,7 +406,7 @@ export async function getComputeDeleteAddRemove( for await (const _ of globalCacheIndex.update( tag, results, - async () => { }, + async () => {}, repoName, )) { } @@ -417,7 +417,7 @@ export async function getComputeDeleteAddRemove( export class GlobalCacheCodeBaseIndex implements CodebaseIndex { relativeExpectedTime: number = 1; - constructor(private db: DatabaseConnection) { } + constructor(private db: DatabaseConnection) {} artifactId = "globalCache"; @@ -472,21 +472,21 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { const SQLITE_MAX_LIKE_PATTERN_LENGTH = 50000; -function truncateFirstNBytes(input: string, maxBytes: number): string { +export function truncateToLastNBytes(input: string, maxBytes: number): string { let bytes = 0; let startIndex = 0; - for (let i = 0; i < input.length; i++) { + for (let i = input.length - 1; i >= 0; i--) { bytes += new TextEncoder().encode(input[i]).length; if (bytes > maxBytes) { - startIndex = i; + startIndex = i + 1; break; } } - return input.substring(startIndex); + return input.substring(startIndex, input.length); } export function truncateSqliteLikePattern(input: string) { - return truncateFirstNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); -} \ No newline at end of file + return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH); +}