# Testing BiznisOS

BiznisOS is tested in four layers. Each layer catches mistakes the one before it can't.

| Layer | Command | Needs | What it catches |
|---|---|---|---|
| 1. Static QA | `python3 tools/qa.py` | Python 3, PHP, Node | Syntax errors, broken Blade, routes that point at missing methods, links to routes or views that don't exist, missing classes, calls to service methods that don't exist, model fields that aren't in the database, `database/biznisos.sql` drifting from the migrations, unknown config keys, broken JavaScript |
| 1b. SQL import | `python3 tools/qa.py --sql` | MySQL or MariaDB running locally | `biznisos.sql` imports into an empty database |
| 2. Unit and feature tests | `php artisan test` | `composer install` | Business rules: permissions, tenant isolation, booking slots, GST, payments, wallet, workflows, inbox, AI tools and more (169 tests) |
| 3. Smoke test | `php artisan test --filter=SmokeTest` | `composer install` | Any page that crashes. It opens every GET page as the owner, a member, a platform admin and a logged-out visitor with one record of everything, and fails on any HTTP 500 |
| 4. CI | push to GitHub | a GitHub repository | Runs all of the above on every push, on SQLite and on MySQL 8, plus migrations up/down, the demo seeder, the SQL import, compiling every Blade view and caching routes |

## Run everything at once

```bash
composer install
cp .env.example .env && php artisan key:generate
tools/test.sh            # static QA + views + routes + PHPUnit
tools/test.sh --mysql    # also imports biznisos.sql into local MySQL
composer check           # the same, through Composer
```

`tools/test.sh` skips the PHP layers and says so if `vendor/` is missing.

## How the test database works

`phpunit.xml` uses SQLite in memory, so tests need no database server and every test starts empty (`RefreshDatabase`). To run the suite on MySQL, set the variables first. Real environment variables take priority over `phpunit.xml`:

```bash
DB_CONNECTION=mysql DB_DATABASE=biznisos_test DB_USERNAME=root DB_PASSWORD=secret php artisan test
```

Never point tests at the live database: `RefreshDatabase` wipes it.

## Third-party services in tests

No test calls a real API. In the `testing` environment:

- Payments (Razorpay, Stripe), WhatsApp, Instagram, SMS (MSG91, Twilio) and AI providers use built-in fake drivers that record what would have been sent.
- Mail uses the `array` mailer and the queue runs synchronously.

You can also turn on a fake driver on staging by setting an integration's credential `driver` to `fake`.

## Writing new tests

- Use `$this->ownerWithAccount()` and `$this->memberOf($account, 'manager')` from `tests/TestCase.php` to set up a business and its people.
- Create records inside a business with `app(CurrentAccount::class)->set($account)` (forget it afterwards), or use `Model::withoutGlobalScopes()->create([... 'account_id' => $account->id])`.
- For every new feature, test the happy path, the permission check (a member is refused), and tenant isolation (another business gets a 404).
- **Adding a page?** The smoke test picks it up automatically. If the route has a new kind of `{parameter}`, the smoke test fails with a message telling you to add it to `params()` in `SmokeTest.php`.
- **Adding a table or column?** Update the migration *and* `database/biznisos.sql`. `tools/qa.py` fails until both match.

## Demo data for manual testing

`php artisan migrate --seed` creates two businesses with contacts, deals, forms and tasks. You can sign in with:

| Email | Role | Password |
|---|---|---|
| owner@biznisos.test | Owner (PIN 482915) | Password123 |
| manager@biznisos.test | Manager | Password123 |
| member@biznisos.test | Member | Password123 |

Use these only on local or staging, never in production.

## Before every release

1. `tools/test.sh --mysql` passes, or CI is green.
2. Click through the main flows on staging with the demo logins: a public form submission, a booking, a checkout in test mode, a WhatsApp reply with the fake driver, and an AI chat.
3. `php artisan migrate --pretend` on a copy of production shows only the expected changes.
