Skip to content
Closed
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
75 changes: 75 additions & 0 deletions apps/server/src/workspace/WorkspaceSearchIndex.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
FileFinder,
type DirItem,
type FileItem,
type GrepCursor,
type GrepOptions,
type GrepResult,
type MixedItem,
} from "@ff-labs/fff-node";
import { afterEach, expect, it } from "@effect/vitest";
import * as Cause from "effect/Cause";
Expand All @@ -30,6 +32,79 @@ function fileItem(relativePath: string): FileItem {
};
}

function mixedFileItem(relativePath: string): MixedItem {
return { type: "file", item: fileItem(relativePath) };
}

function mixedDirectoryItem(relativePath: string): MixedItem {
const item: DirItem = {
relativePath,
dirName: relativePath.slice(relativePath.lastIndexOf("/") + 1),
maxAccessFrecency: 0,
};
return { type: "directory", item };
}

it.effect("deduplicates paths using their wire-normalized value", () =>
Effect.scoped(
Effect.gen(function* () {
const items = [mixedFileItem("pkg/notes.txt"), mixedFileItem("pkg/notes.txt\n")];
const finder = {
destroy: vi.fn(),
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
mixedSearch: vi.fn(() => ({
ok: true as const,
value: {
items,
scores: [],
totalMatched: items.length,
totalFiles: items.length,
},
})),
} as unknown as FileFinder;
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });

const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
const listResult = yield* searchIndex.list();
const searchResult = yield* searchIndex.search("", 10);

expect(listResult.entries.filter((entry) => entry.path === "pkg/notes.txt")).toEqual([
{ kind: "file", path: "pkg/notes.txt" },
]);
expect(searchResult.entries).toEqual([{ kind: "file", path: "pkg/notes.txt" }]);
}),
),
);

it.effect("trims wire whitespace before removing directory separators", () =>
Effect.scoped(
Effect.gen(function* () {
// The file ranks first to prove that a later directory upgrades the
// collision instead of leaving the file kind attached to a real folder.
const items = [mixedFileItem("pkg/ "), mixedDirectoryItem("pkg/")];
const finder = {
destroy: vi.fn(),
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
mixedSearch: vi.fn(() => ({
ok: true as const,
value: {
items,
scores: [],
totalMatched: items.length,
totalFiles: 1,
},
})),
} as unknown as FileFinder;
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });

const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
const result = yield* searchIndex.search("", 10);

expect(result.entries).toEqual([{ kind: "directory", path: "pkg" }]);
}),
),
);

it.effect("filters image searches before applying the result limit", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
60 changes: 37 additions & 23 deletions apps/server/src/workspace/WorkspaceSearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,19 @@ function trimDirectorySeparator(input: string): string {
return input.endsWith("/") ? input.slice(0, -1) : input;
}

function normalizeProjectEntryPath(input: string): string {
// ProjectEntry.path trims at the wire boundary. Trim first so a legal
// whitespace-only final segment cannot leave a separator-only collision.
return trimDirectorySeparator(toPosixPath(input).trim());
}

function parentPathOf(input: string): string | undefined {
const separatorIndex = input.lastIndexOf("/");
return separatorIndex === -1 ? undefined : input.slice(0, separatorIndex);
}

function toProjectEntry(item: MixedItem): ProjectEntry | null {
const normalizedPath = trimDirectorySeparator(toPosixPath(item.item.relativePath));
const normalizedPath = normalizeProjectEntryPath(item.item.relativePath);
if (!normalizedPath) {
return null;
}
Expand All @@ -150,24 +156,44 @@ function toProjectEntry(item: MixedItem): ProjectEntry | null {
}

function toFileEntry(item: FileItem): ProjectEntry | null {
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
const normalizedPath = normalizeProjectEntryPath(item.relativePath);
return normalizedPath ? { path: normalizedPath, kind: "file" } : null;
}

function toDirectoryEntry(item: DirItem): ProjectEntry | null {
const normalizedPath = trimDirectorySeparator(toPosixPath(item.relativePath));
const normalizedPath = normalizeProjectEntryPath(item.relativePath);
return normalizedPath ? { path: normalizedPath, kind: "directory" } : null;
}

function uniqueProjectEntries(entries: Iterable<ProjectEntry | null>): ProjectEntry[] {
const uniqueEntries: ProjectEntry[] = [];
const indexByPath = new Map<string, number>();
for (const entry of entries) {
if (entry === null) continue;
const existingIndex = indexByPath.get(entry.path);
if (existingIndex === undefined) {
indexByPath.set(entry.path, uniqueEntries.length);
uniqueEntries.push(entry);
continue;
}
if (entry.kind === "directory" && uniqueEntries[existingIndex]?.kind === "file") {
uniqueEntries[existingIndex] = entry;
}
}
return uniqueEntries;
Comment thread
ralphsmith80 marked this conversation as resolved.
}

function mapFileSearchResult(
result: SearchResult,
limit: number,
imageOnly = false,
): ProjectSearchEntriesResult {
const entries = result.items.flatMap((item) => {
const entry = toFileEntry(item);
return entry && (!imageOnly || isWorkspaceImagePreviewPath(entry.path)) ? [entry] : [];
});
const entries = uniqueProjectEntries(
result.items.map((item) => {
const entry = toFileEntry(item);
return entry && (!imageOnly || isWorkspaceImagePreviewPath(entry.path)) ? entry : null;
}),
);
return {
entries: entries.slice(0, limit),
truncated: entries.length > limit || result.totalMatched > result.items.length,
Expand All @@ -178,10 +204,7 @@ function mapDirectorySearchResult(
result: DirSearchResult,
limit: number,
): ProjectSearchEntriesResult {
const entries = result.items.flatMap((item) => {
const entry = toDirectoryEntry(item);
return entry ? [entry] : [];
});
const entries = uniqueProjectEntries(result.items.map(toDirectoryEntry));
const rootDirectoryCount = result.items.some((item) => item.relativePath.length === 0) ? 1 : 0;
return {
entries: entries.slice(0, limit),
Expand All @@ -193,25 +216,16 @@ function mapMixedSearchResult(
result: MixedSearchResult,
limit: number,
): { readonly entries: ProjectEntry[]; readonly truncated: boolean } {
const entries: ProjectEntry[] = [];
for (const item of result.items) {
const entry = toProjectEntry(item);
if (entry) {
entries.push(entry);
}
if (entries.length >= limit) {
break;
}
}
const entries = uniqueProjectEntries(result.items.map(toProjectEntry));

const rootDirectoryCount = result.items.some(
(item) => item.type === "directory" && item.item.relativePath.length === 0,
)
? 1
: 0;
return {
entries,
truncated: result.totalMatched - rootDirectoryCount > limit,
entries: entries.slice(0, limit),
truncated: entries.length > limit || result.totalMatched - rootDirectoryCount > limit,
};
}

Expand Down
Loading