|
| 1 | +import { expect, it } from "@effect/vitest"; |
| 2 | +import { ProjectId } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as KeyValueStore from "effect/persistence/KeyValueStore"; |
| 6 | + |
| 7 | +import { MentionIndex, make, mentionIndexScopeKey, type MentionIndexRow } from "./MentionIndex.ts"; |
| 8 | + |
| 9 | +const projectId = ProjectId.make("project-1"); |
| 10 | + |
| 11 | +const row = ( |
| 12 | + partial: Partial<MentionIndexRow> & Pick<MentionIndexRow, "itemKey" | "label">, |
| 13 | +): MentionIndexRow => ({ |
| 14 | + providerId: "pull-request", |
| 15 | + description: partial.description ?? partial.label, |
| 16 | + url: partial.url ?? `https://github.com/org/repo/pull/${partial.itemKey}`, |
| 17 | + tokens: partial.tokens ?? `${partial.itemKey} ${partial.label}`.toLowerCase(), |
| 18 | + updatedAt: partial.updatedAt ?? 1, |
| 19 | + ...partial, |
| 20 | +}); |
| 21 | + |
| 22 | +const testLayer = Layer.effect(MentionIndex, make).pipe(Layer.provide(KeyValueStore.layerMemory)); |
| 23 | + |
| 24 | +it.effect("searches locally without needing a network round trip", () => |
| 25 | + Effect.gen(function* () { |
| 26 | + const index = yield* MentionIndex; |
| 27 | + const scopeKey = mentionIndexScopeKey("pull-request", projectId); |
| 28 | + yield* index.replaceScope(scopeKey, [ |
| 29 | + row({ |
| 30 | + itemKey: "org/repo#42", |
| 31 | + label: "#42", |
| 32 | + description: "Improve context chips", |
| 33 | + tokens: "42 #42 improve context chips org/repo", |
| 34 | + pullRequest: { |
| 35 | + number: 42, |
| 36 | + title: "Improve context chips", |
| 37 | + url: "https://github.com/org/repo/pull/42", |
| 38 | + headBranch: "feat/chips", |
| 39 | + baseBranch: "main", |
| 40 | + state: "open", |
| 41 | + isDraft: false, |
| 42 | + projectId, |
| 43 | + repository: "org/repo", |
| 44 | + }, |
| 45 | + }), |
| 46 | + row({ |
| 47 | + itemKey: "org/repo#7", |
| 48 | + label: "#7", |
| 49 | + description: "Unrelated", |
| 50 | + tokens: "7 #7 unrelated org/repo", |
| 51 | + }), |
| 52 | + ]); |
| 53 | + |
| 54 | + const result = yield* index.search({ |
| 55 | + projectId, |
| 56 | + providers: ["pull-request"], |
| 57 | + query: "context", |
| 58 | + limit: 10, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.status).toBe("ready"); |
| 62 | + expect(result.hits.map((hit) => hit.label)).toEqual(["#42"]); |
| 63 | + expect(result.hits[0]?.pullRequest?.number).toBe(42); |
| 64 | + }).pipe(Effect.provide(testLayer)), |
| 65 | +); |
| 66 | + |
| 67 | +it.effect("ranks an exact number above a substring title hit", () => |
| 68 | + Effect.gen(function* () { |
| 69 | + const index = yield* MentionIndex; |
| 70 | + const scopeKey = mentionIndexScopeKey("pull-request", projectId); |
| 71 | + yield* index.replaceScope(scopeKey, [ |
| 72 | + row({ |
| 73 | + itemKey: "org/repo#420", |
| 74 | + label: "#420", |
| 75 | + tokens: "420 #420 something else", |
| 76 | + }), |
| 77 | + row({ |
| 78 | + itemKey: "org/repo#42", |
| 79 | + label: "#42", |
| 80 | + tokens: "42 #42 exact", |
| 81 | + }), |
| 82 | + ]); |
| 83 | + |
| 84 | + const result = yield* index.search({ |
| 85 | + projectId, |
| 86 | + providers: ["pull-request"], |
| 87 | + query: "42", |
| 88 | + limit: 10, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(result.hits.map((hit) => hit.label)).toEqual(["#42", "#420"]); |
| 92 | + }).pipe(Effect.provide(testLayer)), |
| 93 | +); |
0 commit comments