reverse and test

This commit is contained in:
Nate 2024-10-29 22:23:59 -07:00
parent c9d38cfee1
commit ae636520ca
2 changed files with 42 additions and 8 deletions

View file

@ -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("");
});
});

View file

@ -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);
}
return truncateToLastNBytes(input, SQLITE_MAX_LIKE_PATTERN_LENGTH);
}