Repository navigation
merge: reconcile upstream through 5da4a41d #499
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
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
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,159 @@ | ||
| /** | ||
| * codegraph_explore drops test files from its answer, and the dominant-file | ||
| * and route-file detectors skip them — but each kept a list of test names of | ||
| * its own, so they missed every convention the engine's `isTestFile` learned | ||
| * later: LLVM's `unittests/` trees (a helper there has no test-like name), | ||
| * Google's `foo_unittest.cc`, a C++ `FooTest.cpp`, `e2e/` folders and the | ||
| * examples around a project. On an LLVM-style repo a unit test's harness, named | ||
| * like the code under test, took a file slot in the answer. | ||
| */ | ||
| import { describe, it, expect, afterAll, beforeAll } from 'vitest'; | ||
| import * as fs from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import { CodeGraph } from '../src'; | ||
| import { ToolHandler } from '../src/mcp/tools'; | ||
|
|
||
| const write = (root: string, files: Record<string, string>) => { | ||
| for (const [rel, content] of Object.entries(files)) { | ||
| fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true }); | ||
| fs.writeFileSync(path.join(root, rel), content); | ||
| } | ||
| }; | ||
|
|
||
| const renderedFiles = (text: string): string[] => [...text.matchAll(/^\*\*`([^`]+)`\*\* —/gm)].map((m) => m[1]!); | ||
|
|
||
| describe('codegraph_explore leaves out unit tests the engine knows', () => { | ||
| let root = ''; | ||
| let cg: CodeGraph; | ||
|
|
||
| beforeAll(async () => { | ||
| root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-explore-low-value-')); | ||
| write(root, { | ||
| 'src/lexer.h': `#pragma once | ||
|
|
||
| class Lexer { | ||
| public: | ||
| int nextToken(); | ||
| }; | ||
| `, | ||
| 'src/lexer.cc': `#include "lexer.h" | ||
|
|
||
| int Lexer::nextToken() { return 1; } | ||
| `, | ||
| 'src/parser.h': `#pragma once | ||
|
|
||
| class Parser { | ||
| public: | ||
| int parseExpression(const char* text); | ||
| int parseStatement(const char* text); | ||
| }; | ||
| `, | ||
| 'src/parser.cc': `#include "parser.h" | ||
| #include "lexer.h" | ||
|
|
||
| int Parser::parseExpression(const char* text) { | ||
| Lexer lexer; | ||
| return lexer.nextToken() + (text ? 1 : 0); | ||
| } | ||
|
|
||
| int Parser::parseStatement(const char* text) { | ||
| return parseExpression(text) + 1; | ||
| } | ||
| `, | ||
| // Google's naming: the unittest sits beside the code it tests. | ||
| 'src/parser_unittest.cc': `#include "parser.h" | ||
|
|
||
| void ParsesExpressions() { | ||
| Parser parser; | ||
| parser.parseExpression("1 + 2"); | ||
| parser.parseStatement("x = 1;"); | ||
| } | ||
| `, | ||
| // LLVM's: a unittests/ tree, with a harness named like what it tests. | ||
| 'unittests/Parse/ParserHarness.h': `#pragma once | ||
|
|
||
| #include "../../src/parser.h" | ||
|
|
||
| class ParserHarness { | ||
| public: | ||
| int parseExpression(const char* text) { return parser.parseExpression(text); } | ||
| int parseStatement(const char* text) { return parser.parseStatement(text); } | ||
|
|
||
| private: | ||
| Parser parser; | ||
| }; | ||
| `, | ||
| 'unittests/Parse/ParserTest.cpp': `#include "ParserHarness.h" | ||
|
|
||
| int ParsesStatements() { | ||
| ParserHarness harness; | ||
| return harness.parseStatement("y = 2;") + harness.parseExpression("3"); | ||
| } | ||
| `, | ||
| }); | ||
| cg = await CodeGraph.init(root, { index: true }); | ||
| }, 60_000); | ||
|
|
||
| afterAll(() => { | ||
| cg?.close(); | ||
| if (root) fs.rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('answers from the production files, not the unit tests that call them', async () => { | ||
| const handler = new ToolHandler(cg); | ||
| const result = await handler.execute('codegraph_explore', { query: 'Parser parseExpression parseStatement Lexer nextToken' }); | ||
| const files = renderedFiles(result.content?.[0]?.text ?? ''); | ||
| expect(files).toContain('src/parser.cc'); | ||
| expect(files.filter((f) => /unittests\/|_unittest\.cc$|Test\.cpp$/.test(f))).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('the route-file detector skips an e2e mock server', () => { | ||
| let root = ''; | ||
| let cg: CodeGraph; | ||
|
|
||
| beforeAll(async () => { | ||
| root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-route-low-value-')); | ||
| write(root, { | ||
| 'package.json': JSON.stringify({ name: 'shop', dependencies: { express: '^4.19.0' } }), | ||
| 'src/routes.js': `const express = require('express'); | ||
| const router = express.Router(); | ||
|
|
||
| function listOrders(req, res) { res.json([]); } | ||
| function showOrder(req, res) { res.json({}); } | ||
| function createOrder(req, res) { res.status(201).end(); } | ||
|
|
||
| router.get('/orders', listOrders); | ||
| router.get('/orders/:id', showOrder); | ||
| router.post('/orders', createOrder); | ||
|
|
||
| module.exports = router; | ||
| `, | ||
| // A stand-in payment provider the end-to-end suite starts. | ||
| 'e2e/mock-server.js': `const express = require('express'); | ||
| const app = express(); | ||
|
|
||
| function ok(req, res) { res.json({ ok: true }); } | ||
|
|
||
| app.get('/v1/charges', ok); | ||
| app.post('/v1/charges', ok); | ||
| app.get('/v1/refunds', ok); | ||
| app.post('/v1/refunds', ok); | ||
| app.get('/v1/balance', ok); | ||
|
|
||
| module.exports = app; | ||
| `, | ||
| }); | ||
| cg = await CodeGraph.init(root, { index: true }); | ||
| }, 60_000); | ||
|
|
||
| afterAll(() => { | ||
| cg?.close(); | ||
| if (root) fs.rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("names the app's own routes file", () => { | ||
| expect(cg.getTopRouteFile()?.filePath).toBe('src/routes.js'); | ||
| }); | ||
| }); |
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
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
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.