---
description: Set up k6 and a skeleton load testing project in the current workspace
---

Set up k6 and a runnable skeleton load-testing project in the current workspace root. The goal is to end up with the k6 CLI available and a minimal `tests/` folder with working scripts covering a smoke test and a load test.

## Steps

1. **Inspect the workspace**.
   - Use `bash` to list the current directory and check for `package.json`.
   - Do not touch existing k6 scripts, options files, or CI pipelines that reference k6.

2. **Check k6 installation**.
   - Run `k6 version`.
   - If it prints a version (v0.5x or newer), skip installation.

3. **Install k6 if needed**. Pick the first method that works on the current OS:
   - Windows: `winget install grafana.k6` (or `choco install k6`, or `scoop install k6`).
   - macOS: `brew install k6`.
   - Linux: follow the official apt/dnf instructions from https://grafana.com/docs/k6/latest/set-up/install/.
   - Fallback for any OS: download a binary from the grafana/k6 GitHub releases and put it on `PATH`, or use Docker: `docker run --rm -i grafana/k6 run - < tests/smoke.js`.
   - Re-run `k6 version` to confirm the install. If no method works, create the project files anyway and document the install commands in a `README.md`.

4. **Create the project structure**.
   - Keep it minimal and idiomatic:
     ```
     tests/
       smoke.js
       load.js
     ```
   - Use plain JavaScript; k6 runs scripts natively with no build step.

5. **Create a smoke test**.
   - Write `tests/smoke.js` with a minimal working script:
     ```js
     import http from 'k6/http';
     import { check, sleep } from 'k6';

     const BASE_URL = __ENV.BASE_URL || 'https://quickpizza.grafana.com';

     export const options = {
       vus: 1,
       iterations: 5,
       thresholds: {
         http_req_failed: ['rate<0.01'], // http errors should be less than 1%
         http_req_duration: ['p(95)<500'], // 95% of requests under 500ms
       },
     };

     export default function () {
       const res = http.get(BASE_URL);
       check(res, {
         'status is 200': (r) => r.status === 200,
       });
       sleep(1);
     }
     ```

6. **Create a load test**.
   - Write `tests/load.js` demonstrating executor stages and thresholds:
     ```js
     import http from 'k6/http';
     import { check, sleep } from 'k6';

     const BASE_URL = __ENV.BASE_URL || 'https://quickpizza.grafana.com';

     export const options = {
       scenarios: {
        ramping_vus: {
          executor: 'ramping-vus',
          startVUs: 1,
          stages: [
            { duration: '30s', target: 10 }, // ramp up
            { duration: '1m', target: 10 },  // stay at load
            { duration: '30s', target: 0 },  // ramp down
          ],
          gracefulRampDown: '15s',
        },
       },
       thresholds: {
         http_req_failed: ['rate<0.01'],
         http_req_duration: ['p(95)<500'],
       },
     };

     export default function () {
       const res = http.get(BASE_URL);
       check(res, {
         'status is 200': (r) => r.status === 200,
       });
       sleep(1);
     }
     ```

7. **Add convenience scripts**.
   - If `package.json` is missing, run `npm init -y` first.
   - Use `edit` to add scripts to `package.json`:
     ```json
     {
       "k6:smoke": "k6 run tests/smoke.js",
       "k6:load": "k6 run tests/load.js"
     }
     ```
   - Do not duplicate scripts that already exist.
   - If the workspace has no Node.js at all, write a short `README.md` documenting the raw `k6 run tests/smoke.js` / `k6 run tests/load.js` commands instead.

8. **Verify the setup**.
   - Run `k6 version` and report the version.
   - Run `k6 inspect tests/smoke.js` to confirm the script parses and the options export is picked up.
   - Optionally run `k6 run --iterations 2 tests/smoke.js` for a quick live check (it hits `quickpizza.grafana.com`, so skip if offline). Confirm the threshold summary passes.
   - Report the final k6 version and the list of created files.

## Optional: TypeScript

Only if the user explicitly asks for TypeScript, add esbuild bundling (`k6` has no native TS runtime):
- `npm install -D esbuild typescript` and add a `build` script that bundles `src/**/*.ts` into `tests/`.
- Otherwise skip this step entirely; plain JavaScript is the default.

## Rules

- Do not overwrite existing user files unless they are missing or clearly incomplete.
- Keep the setup minimal and idiomatic; do not add CI pipelines, dashboards, or cloud integrations unless asked.
- Never hardcode secrets or tokens in the scripts; use `__ENV` and environment variables.
- Use `edit` for precise changes to `package.json`.
- Use `write` only for new files or complete rewrites of files you created.

After completing the setup, summarize:
- Which files were created or modified.
- The k6 version installed.
- Any verification results or errors encountered.
