|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `IReranker` / `RERANKER_SERVICE`: the reranker contract the knowledge |
| 4 | +// service resolves per search. The card asks for a type-level pin of the |
| 5 | +// ruled shape: `IReranker { id; rerank(query, documents, opts?: { signal }) }` |
| 6 | +// returning an array of `{ index, score }`, and the `'reranker'` service token, |
| 7 | +// both reachable from the `contracts` barrel. |
| 8 | +// |
| 9 | +// Optionality is pinned with `{} extends Pick<…>` aliases, not `expectTypeOf`: |
| 10 | +// `toEqualTypeOf` cannot tell an optional member from a REQUIRED one typed |
| 11 | +// `T | undefined`. The aliases are exported so TS6196 does not flag them as |
| 12 | +// unused; `check:test-typecheck` compiles this file. |
| 13 | + |
| 14 | +import { describe, it, expect } from 'vitest'; |
| 15 | +import { RERANKER_SERVICE, type IReranker } from './reranker'; |
| 16 | +import { |
| 17 | + RERANKER_SERVICE as RERANKER_SERVICE_FROM_BARREL, |
| 18 | + type IReranker as IRerankerFromBarrel, |
| 19 | +} from './index'; |
| 20 | + |
| 21 | +type Eq<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 22 | +type Assert<T extends true> = T; |
| 23 | + |
| 24 | +type Rerank = IReranker['rerank']; |
| 25 | +type RerankOpts = NonNullable<Parameters<Rerank>[2]>; |
| 26 | + |
| 27 | +/** Exactly two members: `id` and `rerank` (no `model`, no preset list). */ |
| 28 | +export type MembersAreIdAndRerank = Assert<Eq<keyof IReranker, 'id' | 'rerank'>>; |
| 29 | + |
| 30 | +/** `id` is a readonly string, as `IEmbedder.id`. */ |
| 31 | +export type IdIsAString = Assert<Eq<IReranker['id'], string>>; |
| 32 | +export type IdIsReadonly = Assert<Eq<Readonly<Pick<IReranker, 'id'>>, Pick<IReranker, 'id'>>>; |
| 33 | + |
| 34 | +/** `rerank(query, documents, opts?)`: a string query, string documents. */ |
| 35 | +export type RerankTakesQueryDocumentsOpts = Assert< |
| 36 | + Eq<Parameters<Rerank>, [query: string, documents: string[], opts?: { signal?: AbortSignal }]> |
| 37 | +>; |
| 38 | + |
| 39 | +/** The options bag carries only the caller's `AbortSignal`. */ |
| 40 | +export type OptsCarryOnlyASignal = Assert<Eq<RerankOpts, { signal?: AbortSignal }>>; |
| 41 | + |
| 42 | +/** `opts` itself is optional: `rerank(query, documents)` is a valid call. */ |
| 43 | +export type OptsAreOptional = Assert<[string, string[]] extends Parameters<Rerank> ? true : false>; |
| 44 | + |
| 45 | +/** The answer: an array of `{ index, score }`, asynchronously. */ |
| 46 | +export type RerankAnswersIndexAndScore = Assert< |
| 47 | + Eq<ReturnType<Rerank>, Promise<Array<{ index: number; score: number }>>> |
| 48 | +>; |
| 49 | + |
| 50 | +/** The barrel (`@objectstack/spec/contracts`) exports this contract, not a look-alike. */ |
| 51 | +export type BarrelExportsTheContract = Assert<Eq<IRerankerFromBarrel, IReranker>>; |
| 52 | + |
| 53 | +/** The service token is the literal `'reranker'`. */ |
| 54 | +export type TokenIsTheLiteral = Assert<Eq<typeof RERANKER_SERVICE, 'reranker'>>; |
| 55 | + |
| 56 | +describe('IReranker contract', () => { |
| 57 | + it('registers under the `reranker` service token, exported from the contracts barrel', () => { |
| 58 | + expect(RERANKER_SERVICE).toBe('reranker'); |
| 59 | + expect(RERANKER_SERVICE_FROM_BARREL).toBe(RERANKER_SERVICE); |
| 60 | + }); |
| 61 | + |
| 62 | + it('is satisfied by an implementation that answers index + score per document', async () => { |
| 63 | + const reranker: IReranker = { |
| 64 | + id: 'fixture', |
| 65 | + async rerank(_query, documents) { |
| 66 | + return documents.map((_, index) => ({ index, score: 1 / (index + 1) })); |
| 67 | + }, |
| 68 | + }; |
| 69 | + await expect(reranker.rerank('q', ['a', 'b'])).resolves.toEqual([ |
| 70 | + { index: 0, score: 1 }, |
| 71 | + { index: 1, score: 0.5 }, |
| 72 | + ]); |
| 73 | + }); |
| 74 | +}); |
0 commit comments