-
Notifications
You must be signed in to change notification settings - Fork 0
feat(types): port Chunk/SearchResult/ContentType from semble #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Tests for src/types.ts — port-parity with src/semble/tests/test_types.py. | ||
|
|
||
| import { describe, expect, test } from 'bun:test' | ||
| import { | ||
| CallType, | ||
| type Chunk, | ||
| type ChunkDictInput, | ||
| chunkFromDict, | ||
| chunkLocation, | ||
| chunkToDict, | ||
| ContentType, | ||
| type SearchResult, | ||
| searchResultToDict, | ||
| } from './types' | ||
|
|
||
| describe('ContentType', () => { | ||
| test('enum values match the Python str enum', () => { | ||
| expect(ContentType.Code).toBe('code') | ||
| expect(ContentType.Docs).toBe('docs') | ||
| expect(ContentType.Config).toBe('config') | ||
| }) | ||
| }) | ||
|
|
||
| describe('CallType', () => { | ||
| test('enum values match the Python str enum', () => { | ||
| expect(CallType.Search).toBe('search') | ||
| // Python uses `find_related` (snake_case) — telemetry compatibility. | ||
| expect(CallType.FindRelated).toBe('find_related') | ||
| }) | ||
| }) | ||
|
|
||
| describe('chunkLocation', () => { | ||
| test('formats as filePath:startLine-endLine', () => { | ||
| const chunk: Chunk = { | ||
| content: 'x = 1', | ||
| filePath: 'file.ts', | ||
| startLine: 10, | ||
| endLine: 25, | ||
| } | ||
| expect(chunkLocation(chunk)).toBe('file.ts:10-25') | ||
| }) | ||
|
|
||
| test('handles single-line chunks', () => { | ||
| const chunk: Chunk = { | ||
| content: 'x = 1', | ||
| filePath: 'src/a.py', | ||
| startLine: 5, | ||
| endLine: 5, | ||
| } | ||
| expect(chunkLocation(chunk)).toBe('src/a.py:5-5') | ||
| }) | ||
| }) | ||
|
|
||
| describe('chunkToDict / chunkFromDict roundtrip', () => { | ||
| test('preserves all fields with language set', () => { | ||
| const original: Chunk = { | ||
| content: 'function foo() {}', | ||
| filePath: 'src/foo.ts', | ||
| startLine: 1, | ||
| endLine: 3, | ||
| language: 'typescript', | ||
| } | ||
| const dict = chunkToDict(original) | ||
| expect(dict).toEqual({ | ||
| content: 'function foo() {}', | ||
| filePath: 'src/foo.ts', | ||
| startLine: 1, | ||
| endLine: 3, | ||
| language: 'typescript', | ||
| location: 'src/foo.ts:1-3', | ||
| }) | ||
| const reconstructed = chunkFromDict(dict) | ||
| expect(reconstructed).toEqual(original) | ||
| }) | ||
|
|
||
| test('preserves all fields with language omitted (undefined)', () => { | ||
| const original: Chunk = { | ||
| content: 'README content', | ||
| filePath: 'README.md', | ||
| startLine: 1, | ||
| endLine: 10, | ||
| } | ||
| const dict = chunkToDict(original) | ||
| // Python `asdict` emits `None`; we emit `null` to match wire format. | ||
| expect(dict.language).toBeNull() | ||
| expect(dict.location).toBe('README.md:1-10') | ||
|
|
||
| const reconstructed = chunkFromDict(dict) | ||
| expect(reconstructed).toEqual(original) | ||
| expect(reconstructed.language).toBeUndefined() | ||
| }) | ||
|
|
||
| test('chunkFromDict strips location before reconstruction', () => { | ||
| // A malformed `location` must not desync the reconstructed Chunk. | ||
| const reconstructed = chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: 1, | ||
| endLine: 2, | ||
| language: 'ts', | ||
| location: 'WRONG:999-999', | ||
| }) | ||
| // The derived location is recomputed from the line range — never trusted. | ||
| expect(chunkLocation(reconstructed)).toBe('a.ts:1-2') | ||
| }) | ||
|
|
||
| test('chunkFromDict accepts null language (wire format)', () => { | ||
| const reconstructed = chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: 1, | ||
| endLine: 2, | ||
| language: null, | ||
| }) | ||
| expect(reconstructed.language).toBeUndefined() | ||
| }) | ||
|
|
||
| test('chunkFromDict throws on null or non-object input', () => { | ||
| // The compile-time `ChunkDictInput` doesn't reach untrusted JSON callers, | ||
| // so the runtime guard must catch these before they pollute the index. | ||
| expect(() => chunkFromDict(null as unknown as ChunkDictInput)).toThrow(TypeError) | ||
| expect(() => chunkFromDict(undefined as unknown as ChunkDictInput)).toThrow(TypeError) | ||
| expect(() => chunkFromDict('oops' as unknown as ChunkDictInput)).toThrow(TypeError) | ||
| expect(() => chunkFromDict(42 as unknown as ChunkDictInput)).toThrow(TypeError) | ||
| }) | ||
|
|
||
| test('chunkFromDict throws on missing or wrong-typed required fields', () => { | ||
| expect(() => chunkFromDict({} as unknown as ChunkDictInput)).toThrow(TypeError) | ||
| expect(() => | ||
| chunkFromDict({ content: 'x', filePath: 'a.ts', startLine: 1 } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: '1', | ||
| endLine: 2, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 42, | ||
| startLine: 1, | ||
| endLine: 2, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| }) | ||
|
|
||
| test('chunkFromDict throws on NaN or non-finite startLine/endLine', () => { | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: Number.NaN, | ||
| endLine: 2, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: 1, | ||
| endLine: Number.POSITIVE_INFINITY, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: Number.NEGATIVE_INFINITY, | ||
| endLine: 2, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| }) | ||
|
|
||
| test('chunkFromDict throws when language has the wrong type', () => { | ||
| expect(() => | ||
| chunkFromDict({ | ||
| content: 'x', | ||
| filePath: 'a.ts', | ||
| startLine: 1, | ||
| endLine: 2, | ||
| language: 42, | ||
| } as unknown as ChunkDictInput), | ||
| ).toThrow(TypeError) | ||
| }) | ||
| }) | ||
|
|
||
| describe('searchResultToDict', () => { | ||
| test('serialises chunk and score', () => { | ||
| const chunk: Chunk = { | ||
| content: 'def foo():\n pass', | ||
| filePath: 'foo.py', | ||
| startLine: 1, | ||
| endLine: 2, | ||
| language: 'python', | ||
| } | ||
| const result: SearchResult = { chunk, score: 0.87 } | ||
| expect(searchResultToDict(result)).toEqual({ | ||
| chunk: { | ||
| content: 'def foo():\n pass', | ||
| filePath: 'foo.py', | ||
| startLine: 1, | ||
| endLine: 2, | ||
| language: 'python', | ||
| location: 'foo.py:1-2', | ||
| }, | ||
| score: 0.87, | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.