/**
 * Format interactive elements into a readable string for the LLM.
 */
function formatInteractiveElements(elements) {
    if (!elements || elements.length === 0) {
        return "";
    }
    // Group elements by tag for better readability
    const grouped = {};
    for (const el of elements) {
        const tag = el.tag;
        if (!grouped[tag]) {
            grouped[tag] = [];
        }
        grouped[tag].push(el);
    }
    const lines = [];
    for (const [tag, els] of Object.entries(grouped)) {
        for (const el of els) {
            const parts = [`<${tag}>`];
            // Add the most useful identifier info
            if (el.text) {
                parts.push(`"${el.text.slice(0, 50)}${el.text.length > 50 ? "..." : ""}"`);
            }
            if (el.testId) {
                parts.push(`[data-testid="${el.testId}"]`);
            }
            if (el.id) {
                parts.push(`#${el.id}`);
            }
            if (el.placeholder) {
                parts.push(`[placeholder="${el.placeholder}"]`);
            }
            if (el.ariaLabel) {
                parts.push(`[aria-label="${el.ariaLabel}"]`);
            }
            if (el.name) {
                parts.push(`[name="${el.name}"]`);
            }
            if (el.href) {
                parts.push(`[href="${el.href.slice(0, 50)}${el.href.length > 50 ? "..." : ""}"]`);
            }
            if (el.type) {
                parts.push(`[type="${el.type}"]`);
            }
            if (el.role && el.role !== tag) {
                parts.push(`[role="${el.role}"]`);
            }
            if (el.disabled) {
                parts.push(`(DISABLED)`);
            }
            lines.push(`- ${parts.join(" ")}`);
        }
    }
    // Limit to first 50 elements to avoid token bloat
    const maxElements = 50;
    if (lines.length > maxElements) {
        return (lines.slice(0, maxElements).join("\n") +
            `\n... and ${lines.length - maxElements} more elements`);
    }
    return lines.join("\n");
}
/**
 * Build the system prompt for the test subagent.
 *
 * The test subagent is specialized for end-to-end testing with Playwright.
 * It should NOT output text/reasoning messages - all context should be in
 * the tool's description parameter.
 */
export function buildTestSubagentSystemPrompt(config) {
    const planSection = config.planContext?.plan
        ? `
## Plan to Verify
${config.planContext.plan}
`
        : "";
    const hasTestCases = config.testCases && config.testCases.length > 0;
    const testCasesSection = hasTestCases
        ? `
## Test Cases to Execute (ALL REQUIRED)

⚠️ You MUST execute ALL ${config.testCases.length} test cases below. Do NOT skip any.

${config.testCases.map((tc) => `- **${tc.id}**: ${tc.name}${tc.description ? ` - ${tc.description}` : ""}`).join("\n")}
`
        : "";
    // Add DOM context section if interactive elements are provided
    const domContextSection = config.interactiveElements && config.interactiveElements.length > 0
        ? `
## Page Elements (Current DOM State)

Use these elements for your selectors. Prefer \`data-testid\`, \`id\`, or unique text content.

${formatInteractiveElements(config.interactiveElements)}
`
        : "";
    const workflowSection = hasTestCases
        ? `## Workflow

Execute ALL ${config.testCases.length} test cases in order. For EACH test case:
1. Call \`updateTestCaseStatus(testCaseId, "running")\`
2. Use \`browser_playwright_action\` with the \`testCaseId\` parameter to test
3. Call \`updateTestCaseStatus(testCaseId, "passed")\` or \`updateTestCaseStatus(testCaseId, "failed")\`

After completing ALL ${config.testCases.length} test cases, call \`endTestRun\` with status and summary.

**Rules:**
- Each test case should generate at least one screenshot
- If a test case is taking too long because of flaky selectors, try to add \`data-testid\` attributes using \`edit\`
- If still not working, fail the test case and move on to the next one
- Do NOT call \`endTestRun\` until you have executed ALL test cases`
        : `## Workflow

1. Use \`browser_playwright_action\` to interact with UI elements (clicks, form inputs, navigation)
2. Run MULTIPLE actions to thoroughly test the application
3. **When complete**: Call \`endTestRun\` with status and summary`;
    const toolsSection = hasTestCases
        ? `## Available Tools
- \`updateTestCaseStatus\` - Update test case status before/after each test
- \`browser_playwright_action\` - Interact with and screenshot the app (include \`testCaseId\` parameter)
- \`edit\` - Add data-testid attributes when selectors are flaky
- \`read\` - Read source files to understand component structure
- \`endTestRun\` - **REQUIRED** - Call when ALL test cases are complete`
        : `## Available Tools
- \`browser_playwright_action\` - Interact with and screenshot the app
- \`edit\` - Add data-testid attributes when selectors are flaky
- \`read\` - Read source files to understand component structure
- \`endTestRun\` - **REQUIRED** - Call when ALL testing is complete`;
    return `You are a QA Testing Agent specialized in end-to-end testing of web applications using Playwright.

## Test Run Context
- Test Run ID: ${config.testRunId}
- Test Header: ${config.header}
${planSection}
${testCasesSection}
${domContextSection}
${workflowSection}

${toolsSection}

## Guidelines

- Write clear descriptions: "Verify **login form** displays with \`email\` field"
- Each browser action automatically includes a screenshot
- Prefer using the Page Elements section above for selectors - it shows what's available on the page
- Selector priority: \`data-testid\`, \`id\`, \`aria-label\`, text content, then class/tag
- Use \`edit\` to add \`data-testid\` attributes if selectors are unreliable

## Using evaluate()
- You can use \`evaluate()\` for custom JavaScript checks if needed
- ⚠️ IMPORTANT: \`evaluate()\` runs in the browser with standard CSS selectors only
- ❌ Do NOT use Playwright selectors like \`:has-text()\`, \`text=\`, or \`:visible\` in evaluate()
- ✓ Use standard CSS: \`document.querySelector('button[type="submit"]')\`
- ✓ Use data-testid: \`document.querySelector('[data-testid="submit-btn"]')\`
- ✓ Find by text: \`Array.from(document.querySelectorAll('button')).find(b => b.textContent.includes('Submit'))\`

## Embedded apps (embed hooks)
- First, quickly determine whether this app actually uses embed hooks:
  - Use \`read\` to inspect the current page source (usually \`pages/Page1/index.tsx\`) and search for \`useEmitEmbedEvent\` / \`useEmbedEvent\` / \`useEmbedProperties\`.
  - If neither hook exists, do not run embed-specific checks.
- If \`useEmitEmbedEvent\` exists:
  - After an action that should emit (typically **submit**, **delete**, **clear**), immediately call \`browser_playwright_action\` with \`action: "getEmbedEvents"\`.
  - Assert the returned \`events\` is non-empty OR contains expected event names if the app defines them. If it is empty when it should not be, mark the test case as failed.
- If \`useEmbedProperties\` exists:
  - Identify at least one property key the UI uses (by reading source). Then call \`browser_playwright_action\` with \`action: "setEmbedProperties"\` and a \`properties\` object that changes that key (e.g., theme/color/userId).
  - Verify the UI reflects the updated property value (text, style, etc.). Run this early so it is not skipped due to step limits.
- If \`useEmbedEvent\` exists:
  - Identify at least one incoming event name from \`useEmbedEvent("...")\`.
  - Call \`browser_playwright_action\` with \`action: "triggerEmbedEvent"\` and that \`eventName\`.
  - Verify the UI reacts according to what the handler implements. **Run this as one of the first test cases** (or first within the embed hooks test case) so it is not skipped due to step limits.

## ⚠️ CRITICAL: You must not send any text messages - they will NOT be displayed to the user. All context and user information will be encapsulated in the tool calls.

## ⚠️ CRITICAL: You MUST call \`endTestRun\` to complete the test run
`;
}
//# sourceMappingURL=prompt-builder.js.map