|
| 1 | +# Test Relation API |
| 2 | + |
| 3 | +The Test Relation API bridges the gap between production code and test code. It helps Agents perform targeted verification ("I changed X, what should I run?") and understand test failures ("Test Y failed, what does it cover?"). |
| 4 | + |
| 5 | +## TestRelationRequest |
| 6 | + |
| 7 | +| Field | Type | Default | Description | |
| 8 | +| :---------- | :---------------------- | :---------- | :-------------------------------------------------------- | |
| 9 | +| `locate` | [`Locate`](locate.md) | Required | The symbol to analyze (source function or test case). | |
| 10 | +| `direction` | `"to_test" \| "to_source"` | `"to_test"` | Find tests for source, or find source for tests. | |
| 11 | + |
| 12 | +## TestRelationResponse |
| 13 | + |
| 14 | +| Field | Type | Description | |
| 15 | +| :-------------- | :------------------- | :--------------------------------------------- | |
| 16 | +| `symbol` | `SymbolInfo` | The resolved input symbol. | |
| 17 | +| `direction` | `string` | The direction used. | |
| 18 | +| `related_items` | `TestRelationItem[]` | List of found related items (tests or source). | |
| 19 | + |
| 20 | +### TestRelationItem |
| 21 | + |
| 22 | +| Field | Type | Description | |
| 23 | +| :---------- | :------- | :---------------------------------------------------------------------- | |
| 24 | +| `name` | `string` | Name of the related item (e.g., `test_add`). | |
| 25 | +| `kind` | `string` | Symbol kind (e.g., `function`, `class`). | |
| 26 | +| `file_path` | `string` | Path to the file. | |
| 27 | +| `range` | `Range` | Location in the file. | |
| 28 | +| `strategy` | `string` | How the relation was found: `reference`, `convention`, or `import`. | |
| 29 | + |
| 30 | +## Implementation Guide |
| 31 | + |
| 32 | +This API uses a multi-strategy approach to find relations, combining LSP capabilities with heuristic rules. |
| 33 | + |
| 34 | +### Direction: `to_test` (Source -> Test) |
| 35 | + |
| 36 | +1. **LSP References (Strongest Signal)**: |
| 37 | + * Call `textDocument/references` on the source symbol. |
| 38 | + * Filter results: Keep files located in `tests/` directories or matching pattern `*test*`/`*spec*`. |
| 39 | + * Mark as `strategy: reference`. |
| 40 | +2. **Naming Convention (Heuristic)**: |
| 41 | + * Infer potential test names (e.g., `add` -> `test_add`, `TestAdd`). |
| 42 | + * Use `workspace/symbol` to search for these names. |
| 43 | + * Mark as `strategy: convention`. |
| 44 | +3. **Module Imports (Broadest Signal)**: |
| 45 | + * Identify which test files import the source module. |
| 46 | + * Mark as `strategy: import`. |
| 47 | + |
| 48 | +### Direction: `to_source` (Test -> Source) |
| 49 | + |
| 50 | +1. **LSP Outgoing Calls (Strongest Signal)**: |
| 51 | + * Analyze the test function body. |
| 52 | + * Use `textDocument/definition` on symbols used within the test. |
| 53 | + * Filter results: Keep symbols defined in the project's source directory (exclude external libs). |
| 54 | + * Mark as `strategy: reference`. |
| 55 | +2. **Naming Convention (Heuristic)**: |
| 56 | + * Infer source name from test name (e.g., `test_add` -> `add`). |
| 57 | + * Use `workspace/symbol` to search. |
| 58 | + * Mark as `strategy: convention`. |
| 59 | + |
| 60 | +## Example Usage |
| 61 | + |
| 62 | +### Scenario 1: Finding tests for a modified function |
| 63 | + |
| 64 | +#### Request |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "locate": { |
| 69 | + "file_path": "src/calculator.py", |
| 70 | + "scope": { |
| 71 | + "symbol_path": ["add"] |
| 72 | + } |
| 73 | + }, |
| 74 | + "direction": "to_test" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +#### Markdown Rendered for LLM |
| 79 | + |
| 80 | +```markdown |
| 81 | +# Test Relation for `add` (to_test) |
| 82 | + |
| 83 | +Found 2 related item(s): |
| 84 | + |
| 85 | +- **test_add_basic** (`function`) |
| 86 | + - File: `tests/test_calculator.py` |
| 87 | + - Line: 15 |
| 88 | + - Strategy: `convention` |
| 89 | +- **test_add_edge_cases** (`function`) |
| 90 | + - File: `tests/test_calculator.py` |
| 91 | + - Line: 28 |
| 92 | + - Strategy: `reference` |
| 93 | +``` |
| 94 | + |
| 95 | +### Scenario 2: Finding source code for a failing test |
| 96 | + |
| 97 | +#### Request |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "locate": { |
| 102 | + "file_path": "tests/test_auth.py", |
| 103 | + "scope": { |
| 104 | + "symbol_path": ["TestLogin", "test_invalid_password"] |
| 105 | + } |
| 106 | + }, |
| 107 | + "direction": "to_source" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +#### Markdown Rendered for LLM |
| 112 | + |
| 113 | +```markdown |
| 114 | +# Test Relation for `test_invalid_password` (to_source) |
| 115 | + |
| 116 | +Found 1 related item(s): |
| 117 | + |
| 118 | +- **AuthService.login** (`method`) |
| 119 | + - File: `src/services/auth.py` |
| 120 | + - Line: 42 |
| 121 | + - Strategy: `reference` |
| 122 | +``` |
0 commit comments