Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import {
buildFetchProgress,
buildLineResolutionProgress,
deriveSegmentText,
fetchAllResults,
fetchRepoTeams,
searchCode,
Expand All @@ -28,6 +29,26 @@ describe("segmentLineCol (api)", () => {
});
});

// ─── deriveSegmentText (issue #151, restores PR #133) ─────────────────────────────────────

describe("deriveSegmentText", () => {
it("returns seg.text unchanged when GitHub provides it", () => {
expect(deriveSegmentText("hello world", { text: "hello", indices: [0, 5] })).toBe("hello");
});

it("derives the text from fragment + indices when seg.text is omitted", () => {
expect(deriveSegmentText("hello world", { indices: [6, 11] })).toBe("world");
});

it("returns an empty string for out-of-range indices", () => {
expect(deriveSegmentText("hello", { indices: [10, 20] })).toBe("");
});

it("returns an empty string when end is before start", () => {
expect(deriveSegmentText("hello", { indices: [4, 1] })).toBe("");
});
});

// ─── searchCode ───────────────────────────────────────────────────────────────

describe("searchCode", () => {
Expand Down Expand Up @@ -146,6 +167,28 @@ describe("fetchAllResults", () => {
expect(results[0].textMatches[0].matches[0].col).toBe(1);
});

it("derives matchedText from fragment + indices when GitHub omits the segment's text field (issue #151)", async () => {
const fakeItem = {
path: "src/foo.ts",
html_url: "https://github.com/org/repo/blob/main/src/foo.ts",
repository: { full_name: "org/repo", archived: false },
Comment on lines +170 to +174
text_matches: [
{
fragment: "hello world",
matches: [{ indices: [6, 11] }],
},
],
};
globalThis.fetch = (async () =>
new Response(JSON.stringify({ items: [fakeItem], total_count: 1 }), {
status: 200,
headers: { "content-type": "application/json" },
})) as typeof fetch;

const results = await fetchAllResults("world", "org", "tok");
expect(results[0].textMatches[0].matches[0].text).toBe("world");
});

it("marks archived repos correctly", async () => {
const fakeItem = {
path: "lib/old.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ export function segmentLineCol(fragment: string, offset: number): { line: number
};
}

/**
* Derive a text-match segment's matched text from `fragment` and `indices`
* when GitHub omits the `text` field on the segment. Restores PR #133 —
* see issue #151.
*/
export function deriveSegmentText(fragment: string, seg: RawSearchSegment): string {
if (seg.text !== undefined) return seg.text;
const [start, end] = seg.indices;
if (start < 0 || end < start || end > fragment.length) return "";
return fragment.slice(start, end);
}

export async function searchCode(
q: string,
org: string,
Expand Down Expand Up @@ -296,7 +308,7 @@ export async function fetchAllResults(
const indices = seg.indices;
const { line: fragLine, col } = segmentLineCol(fragment, indices[0]);
const line = fragmentStartLine + fragLine - 1;
return { text: seg.text ?? "", indices, line, col };
return { text: deriveSegmentText(fragment, seg), indices, line, col };
}),
};
}),
Expand Down
Loading