Skip to content

Commit 0be6595

Browse files
committed
Add test related changes
1 parent 479a08e commit 0be6595

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

workspaces/ballerina/ballerina-extension/src/features/ai/service/test/function_tests.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ import { getErrorMessage } from "../utils";
1919
import { generateTest, getDiagnostics } from "../../testGenerator";
2020
import { URI } from "vscode-uri";
2121
import * as fs from "fs";
22+
import * as path from "path";
2223
import { CopilotEventHandler, createWebviewEventHandler } from "../event";
2324
import { getCurrentProjectRoot } from "../../../../utils/project-utils";
25+
import { StateMachine } from "../../../../stateMachine";
2426

2527
// Core function test generation that emits events
2628
export async function generateFunctionTestsCore(
2729
params: TestGeneratorIntermediaryState,
2830
eventHandler: CopilotEventHandler
2931
): Promise<void> {
30-
const testPath = "tests/test.bal";
3132
const functionIdentifier = params.resourceFunction;
3233

3334
eventHandler({
@@ -48,6 +49,22 @@ export async function generateFunctionTestsCore(
4849
return;
4950
}
5051

52+
// Compute workspace-relative paths for test files (for display in UI)
53+
const context = StateMachine.context();
54+
const workspacePath = context.workspacePath;
55+
let testPathForDisplay = "tests/test.bal";
56+
let configPathForDisplay = "tests/Config.toml";
57+
58+
if (workspacePath) {
59+
// Workspace project: include package path prefix (e.g., "foo/tests/test.bal")
60+
const relativeProjectPath = path.relative(workspacePath, projectPath);
61+
testPathForDisplay = path.join(relativeProjectPath, "tests/test.bal");
62+
configPathForDisplay = path.join(relativeProjectPath, "tests/Config.toml");
63+
}
64+
65+
// Use project-relative path for file operations
66+
const testPath = "tests/test.bal";
67+
5168
const response = await generateTest(projectPath, {
5269
targetType: TestGenerationTarget.Function,
5370
targetIdentifier: functionIdentifier,
@@ -102,12 +119,12 @@ export async function generateFunctionTestsCore(
102119
});
103120
eventHandler({
104121
type: "content_block",
105-
content: `\n\n<code filename="${testPath}" type="test">\n\`\`\`ballerina\n${testCode}\n\`\`\`\n</code>`,
122+
content: `\n\n<code filename="${testPathForDisplay}" type="test">\n\`\`\`ballerina\n${testCode}\n\`\`\`\n</code>`,
106123
});
107124
if (testConfig) {
108125
eventHandler({
109126
type: "content_block",
110-
content: `\n\n<code filename="tests/Config.toml" type="test">\n\`\`\`ballerina\n${testConfig}\n\`\`\`\n</code>`,
127+
content: `\n\n<code filename="${configPathForDisplay}" type="test">\n\`\`\`ballerina\n${testConfig}\n\`\`\`\n</code>`,
111128
});
112129
}
113130

workspaces/ballerina/ballerina-extension/src/features/ai/service/test/test_plan.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { generateTest, getDiagnostics } from "../../testGenerator";
2222
import { CopilotEventHandler, createWebviewEventHandler } from "../event";
2323
import { AIPanelAbortController } from "../../../../../src/rpc-managers/ai-panel/utils";
2424
import { getCurrentProjectRoot } from "../../../../utils/project-utils";
25+
import { StateMachine } from "../../../../stateMachine";
26+
import * as path from "path";
2527

2628
export interface TestPlanResponse {
2729
testPlan: string;
@@ -176,6 +178,20 @@ export async function generateTestPlanCore(
176178
eventHandler({ type: "error", content: getErrorMessage(error) });
177179
return;
178180
}
181+
182+
// Compute workspace-relative paths for test files
183+
const context = StateMachine.context();
184+
const workspacePath = context.workspacePath;
185+
let testPath = "tests/test.bal";
186+
let configPath = "tests/Config.toml";
187+
188+
if (workspacePath) {
189+
// Workspace project: include package path prefix (e.g., "foo/tests/test.bal")
190+
const relativeProjectPath = path.relative(workspacePath, projectPath);
191+
testPath = path.join(relativeProjectPath, "tests/test.bal");
192+
configPath = path.join(relativeProjectPath, "tests/Config.toml");
193+
}
194+
179195
const testResp = await generateTest(projectPath, {
180196
targetType: TestGenerationTarget.Service,
181197
targetIdentifier: target,
@@ -209,12 +225,12 @@ export async function generateTestPlanCore(
209225
});
210226
eventHandler({
211227
type: "content_block",
212-
content: `\n\n<code filename="tests/test.bal" type="test">\n\`\`\`ballerina\n${testCode}\n\`\`\`\n</code>`,
228+
content: `\n\n<code filename="${testPath}" type="test">\n\`\`\`ballerina\n${testCode}\n\`\`\`\n</code>`,
213229
});
214230
if (testConfig) {
215231
eventHandler({
216232
type: "content_block",
217-
content: `\n\n<code filename="tests/Config.toml" type="test">\n\`\`\`ballerina\n${testConfig}\n\`\`\`\n</code>`,
233+
content: `\n\n<code filename="${configPath}" type="test">\n\`\`\`ballerina\n${testConfig}\n\`\`\`\n</code>`,
218234
});
219235
}
220236
eventHandler({ type: "stop", command: Command.Tests });

0 commit comments

Comments
 (0)