/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { workflow } from '../workflow-generator.js';
export function generateQaWorkflows(version) {
    return {
        'session-from-cli.md': workflow(version, 'Salesforce CLI Session Authentication', `
## Overview

This workflow shows how to authenticate Playwright tests using the active
SF CLI session — no passwords, no \`.env\` credentials. The access token is
read directly from \`sf org display\` via \`execFileSync\` (no shell interpolation).

## Prerequisites

| Tool | Check | Install |
|------|-------|---------|
| Salesforce CLI | \`sf --version\` | [https://developer.salesforce.com/tools/salesforcecli](https://developer.salesforce.com/tools/salesforcecli) |
| @playwright/test | \`npx playwright --version\` | \`npm install --save-dev @playwright/test && npx playwright install\` |

Verify you have an authenticated org:
\`\`\`bash
sf org display --json | grep instanceUrl
\`\`\`

## 1. Scaffold the Utilities (One-Time Setup)

Run setup-agents to generate \`src/utils/salesforce-auth.ts\` and
\`src/utils/salesforce-api.ts\` in your project:

\`\`\`bash
sf setup-agents local --profile qa
\`\`\`

Both files are written to \`src/utils/\`. They are not overwritten unless you
pass \`--force\`.

## 2. Use in Tests

### Pattern A — Full navigation (recommended for UI tests)

\`\`\`typescript
import { test, expect } from '@playwright/test';
import { navigateWithCLISession } from '../src/utils/salesforce-auth';

test.describe('Case Management', () => {
  test.beforeEach(async ({ page, context }) => {
    await navigateWithCLISession(page, context);
  });

  test('should show cases list', async ({ page }) => {
    await page.goto('/lightning/o/Case/list');
    await expect(page.getByRole('button', { name: 'New' })).toBeVisible();
  });
});
\`\`\`

### Pattern B — Context-only (for API-heavy tests)

\`\`\`typescript
import { test } from '@playwright/test';
import { useSalesforceCLISession } from '../src/utils/salesforce-auth';
import { createRecord, deleteRecord } from '../src/utils/salesforce-api';

test('creates and deletes a case', async ({ page, context }) => {
  await useSalesforceCLISession(context);
  const caseId = await createRecord(page, 'Case', { Subject: 'Playwright Test', Status: 'New' });
  // ... assertions ...
  await deleteRecord(page, 'Case', caseId);
});
\`\`\`

### Pattern C — Fixture (for multi-test suites)

\`\`\`typescript
// fixtures/sfFixture.ts
import { test as base } from '@playwright/test';
import { navigateWithCLISession } from '../src/utils/salesforce-auth';

export const test = base.extend({
  sfPage: async ({ page, context }, use) => {
    await navigateWithCLISession(page, context);
    await use(page);
  },
});
\`\`\`

## 3. Targeting a Specific Org

Set \`SF_TARGET_ORG\` to use a named org or alias instead of the default:

\`\`\`bash
SF_TARGET_ORG=qa-sandbox npx playwright test
\`\`\`

Or in \`playwright.config.ts\`:
\`\`\`typescript
process.env.SF_TARGET_ORG = 'qa-sandbox';
\`\`\`

## 4. CI/CD (No SF CLI Available)

In CI, inject the org URL and token as environment variables instead:

\`\`\`bash
SALESFORCE_INSTANCE_URL=https://myorg.my.salesforce.com
SALESFORCE_ACCESS_TOKEN=00D...
\`\`\`

Modify \`getSalesforceOrgInfo()\` to check these env vars first:
\`\`\`typescript
if (process.env.SALESFORCE_INSTANCE_URL && process.env.SALESFORCE_ACCESS_TOKEN) {
  return {
    instanceUrl: process.env.SALESFORCE_INSTANCE_URL,
    accessToken: process.env.SALESFORCE_ACCESS_TOKEN,
    username: process.env.SALESFORCE_USERNAME ?? 'ci-user',
    orgId: process.env.SALESFORCE_ORG_ID ?? '',
  };
}
\`\`\`
`),
        'run-playwright.md': workflow(version, 'Run Playwright Tests', `
## Prerequisites

| Tool | Check | Install |
|------|-------|---------|
| Node.js >= 18 | \`node --version\` | [https://nodejs.org](https://nodejs.org) |
| @playwright/test | \`npx playwright --version\` | \`npm install --save-dev @playwright/test && npx playwright install\` |

Before running tests, verify Playwright is installed:

\`\`\`bash
npx playwright --version 2>/dev/null || (echo "Installing Playwright..." && npm install --save-dev @playwright/test && npx playwright install)
\`\`\`

## 1. Confirm Environment

Ask which org / environment to test against. Validate the \`.env\` file has the required credentials.

## 2. Run Tests

\`\`\`bash
# All tests
npx playwright test

# Specific test file
npx playwright test tests/<feature>.spec.ts

# Specific tag
npx playwright test --grep "@smoke"
\`\`\`

## 3. Analyse Results

If tests fail:
- Read the error message and screenshot (captured automatically on failure)
- Check if the failure is a flake (retry once) or a real regression
- Identify the Page Object responsible for the failed selector

## 4. Open Report

\`\`\`bash
npx playwright show-report
\`\`\`

Report pass/fail summary and any accessibility violations found by axe-core.
`),
        'agent-dml-assertions.md': workflow(version, 'Agentforce E2E DML Assertion Pattern', `
## Overview

Agentforce tests must validate **two layers**:
1. **Conversational output** — the agent's text reply in the chat.
2. **DML side effect** — every record the agent Action is expected to create, update, or delete,
   queried via \`sf data query\` and compared to expected field values.

A green chat reply is NOT proof the underlying Action ran successfully.

## Scaffolded Helpers

These files are generated by \`sf setup-agents local --profile qa\`:

- \`test/utils/sf-cli-client.ts\` — typed SF CLI wrapper (query, runApex, deleteRecord)
- \`test/utils/retry-until.ts\` — bounded retry for async DML (Queueable / Platform Events)

## Pattern

\`\`\`typescript
import { test, expect } from '@playwright/test';
import { SfCliClient } from '../utils/sf-cli-client';
import { retryUntil } from '../utils/retry-until';
import { navigateWithCLISession } from '../utils/salesforce-auth';

test('PHP_CreateMaintenanceCase — assigns case to PHP Maintenance queue @agent-dml', async ({ page, context }) => {
  await navigateWithCLISession(page, context);
  const client = new SfCliClient();
  const createdIds: string[] = [];

  // 1. Conversational layer — interact with agent and assert reply
  await page.goto('/lightning/o/Case/new');
  // ... (navigate to agent chat, send message) ...
  await expect(page.locator('[data-testid="agent-reply"]')).toContainText('Case created');

  // 2. DML layer — verify the record was actually written correctly
  const cases = await retryUntil(
    () => client.query<{ Id: string; OwnerId: string }>(\`SELECT Id, OwnerId FROM Case WHERE Subject = 'Maintenance Request' ORDER BY CreatedDate DESC LIMIT 1\`),
    (rows) => rows.length > 0,
    { timeoutMs: 10_000, intervalMs: 500 }
  );
  expect(cases).toHaveLength(1);
  expect(cases[0].OwnerId).toBe(process.env.PHP_MAINTENANCE_QUEUE_ID); // assert correct owner, not running user

  createdIds.push(cases[0].Id);

  // 3. Cleanup — delete every record created by this test
  afterEach(async () => {
    for (const id of createdIds) await client.deleteRecord('Case', id);
  });
});
\`\`\`

## Evidence Persistence

After querying, save the result as evidence:
\`\`\`bash
# Persist the query output automatically via SfCliClient option
const cases = await client.query('SELECT ...', { persistToFile: 'test-results/create-maintenance-case-dml.json' });
\`\`\`

## CI Pipeline Lane

Tag all DML-asserting tests with \`@agent-dml\`. Run them on a dedicated CI lane:
\`\`\`bash
npx playwright test --grep "@agent-dml"
\`\`\`

Wire this in \`.github/workflows/playwright.yml\`:
\`\`\`yaml
- name: Run agent DML assertions
  run: npx playwright test --grep "@agent-dml"
  env:
    SF_TARGET_ORG: \${{ secrets.SF_QA_SANDBOX_ALIAS }}
    PHP_MAINTENANCE_QUEUE_ID: \${{ secrets.PHP_MAINTENANCE_QUEUE_ID }}
\`\`\`
`),
        'generate-test-report.md': workflow(version, 'Generate QA Test Report', `
## Prerequisites

| Tool | Check | Install |
|------|-------|---------|
| @playwright/test | \`npx playwright --version\` | \`npm install --save-dev @playwright/test && npx playwright install\` |

## 1. Run Full Suite with Trace

\`\`\`bash
npx playwright test --reporter=html --trace=on
\`\`\`

## 2. Collect Results

Read the \`playwright-report/index.html\` output:
- Total tests: passed / failed / skipped
- Duration
- Screenshots and traces for failing tests
- Accessibility violations summary

## 3. Summarise

Generate a markdown summary table:

| Suite | Passed | Failed | Skipped | Coverage |
|-------|--------|--------|---------|----------|
| ...   | ...    | ...    | ...     | ...      |

Attach to PR description or Jira ticket as requested by the developer.
`),
    };
}
//# sourceMappingURL=qa.js.map