|
| 1 | +# Copilot Instructions — Mailbox for Laravel (Repository‑wide) |
| 2 | + |
| 3 | +> Place this file at **`.github/copilot-instructions.md`** in the repository root so Copilot (Chat, PR review, code suggestions, and Coding Agent) loads it automatically. |
| 4 | +
|
| 5 | +## Project overview |
| 6 | + |
| 7 | +* This repository is a **Laravel package** that embeds a local email inbox for development and QA, similar to Mailtrap but self‑contained. It ships a Vue‑powered dashboard, storage drivers, a custom mail transport, and HTTP controllers. |
| 8 | +* Primary goals: zero‑config local email capture, a clean UI, and **full automated test coverage** for every class and feature. |
| 9 | + |
| 10 | +## Tech stack & versions (default) |
| 11 | + |
| 12 | +* **PHP**: ^8.2 (strict types, typed properties, readonly where applicable) |
| 13 | +* **Laravel**: ^10 | ^11 |
| 14 | +* **Testing**: Pest 3/4, Laravel test helpers; Coverage target: **90%+ lines**, **80%+ branches** |
| 15 | +* **Static analysis**: PHPStan (Larastan) level ≥ 6; Psalm optional |
| 16 | +* **Code style**: Laravel Pint (PSR‑12), EditorConfig |
| 17 | +* **Front end**: Vue 3 + Vite, TypeScript (for any new scripts), TailwindCSS with **scoped/prefixed classes** to avoid collisions with host apps |
| 18 | +* **Tooling**: ESLint + Prettier (for JS/TS/Vue), Stylelint (optional) |
| 19 | + |
| 20 | +## How to build & test (for Copilot to follow) |
| 21 | + |
| 22 | +* **Install PHP deps**: `composer install` |
| 23 | +* **Install Node deps**: `npm ci` |
| 24 | +* **Build assets for the package**: `npm run build` |
| 25 | +* **Run tests (fast)**: `./vendor/bin/pest -p` |
| 26 | +* **Run tests (coverage)**: `./vendor/bin/pest --coverage --min=90` |
| 27 | +* **Static analysis**: `./vendor/bin/phpstan analyse` |
| 28 | +* **Lint**: `./vendor/bin/pint -v` |
| 29 | + |
| 30 | +## Architecture (high level) |
| 31 | + |
| 32 | +* **Transport**: `InboxTransport` captures `SentMessage`, normalizes it, stores payload, optionally decorates another transport. |
| 33 | +* **Capture & Storage**: |
| 34 | + |
| 35 | + * `CaptureService` generates deterministic keys, persists payloads with metadata, lists/paginates, update, clear. |
| 36 | + * `MessageStore` (contract) with a default `FileStorage` driver. |
| 37 | + * `StoreManager` resolves drivers via config & custom resolvers. |
| 38 | +* **HTTP**: `InboxController`, `SendTestMailController`, `ClearInboxController`, `SeenController`, `AssetController` (static assets), `AuthorizeInboxMiddleware` (route gating). |
| 39 | +* **Support**: `MessageNormalizer` (creates canonical payload), `InboxServiceProvider`, `InstallCommand` (publishes assets/config/routes), `config/inbox.php`. |
| 40 | + |
| 41 | +## Directory & naming conventions |
| 42 | + |
| 43 | +* **Namespaces**: `Redberry\MailboxForLaravel\...` |
| 44 | +* **Contracts** under `Contracts/`; **Support** under `Support/` |
| 45 | +* **HTTP** under `Http/Controllers` and `Http/Middleware` |
| 46 | +* **Storage drivers** under `Storage/` |
| 47 | +* **Tests** mirror src namespaces: `tests/Unit`, `tests/Feature`, `tests/Arch` |
| 48 | +* **Vue** in `resources/js/mailbox` (or equivalent), single entry `resources/js/mailbox.js` |
| 49 | + |
| 50 | +## Coding standards for Copilot |
| 51 | + |
| 52 | +### PHP & Laravel |
| 53 | + |
| 54 | +* Prefer **value objects**, **enums**, and **DTOs** over loose arrays where practical. |
| 55 | +* Always add **return types** and **param types**. No mixed unless unavoidable; document with PHPDoc if union types are complex. |
| 56 | +* **Never** call `env()` outside `config/` files. Use `config()` or typed config accessors. |
| 57 | +* Avoid facades in core services; depend on **interfaces** and constructor injection; keep controllers thin. |
| 58 | +* **Immutable data** where possible (`readonly` properties, new instances vs mutation). |
| 59 | +* **No hidden state**: services should be stateless; transport may store the last key but avoid global singletons. |
| 60 | +* Throw **domain exceptions** for invalid states; no silent failures. |
| 61 | +* Use **`Response::view()` / `view()`** with view models; no heavy logic in Blade. |
| 62 | +* File IO via storage drivers; never touch `storage_path()` directly from controllers. |
| 63 | +* Prefer **`Clock`** abstraction (or now() injection) for time; avoid `time()` directly in business logic; guard with tests. |
| 64 | + |
| 65 | +### Vue / TS / Vite |
| 66 | + |
| 67 | +* Use **script setup** and **defineProps/defineEmits**. |
| 68 | +* Use **TypeScript** for new files; define `Message`, `Envelope`, etc. interfaces. |
| 69 | +* Stateless, presentational components; lift state to a store if needed (Pinia optional). |
| 70 | +* Accessibility: semantic elements, keyboard navigation, focus rings. |
| 71 | + |
| 72 | +### Security & safety |
| 73 | + |
| 74 | +* Do not render raw HTML without sanitization in the dashboard. |
| 75 | +* Treat captured emails as **non‑production** only; redact secrets in UI where possible. |
| 76 | +* Avoid writing to arbitrary paths; storage driver paths must be constrained to package directory. |
| 77 | + |
| 78 | +## Test policy (Copilot must generate tests by default) |
| 79 | + |
| 80 | +> **Everything is tested**: every class, controller action, middleware path, transport behavior, and storage driver. |
| 81 | +
|
| 82 | +### Coverage & gates |
| 83 | + |
| 84 | +* Generate **unit tests** for all services, contracts, and drivers. |
| 85 | +* Generate **feature tests** for HTTP routes, middleware, publishing/installation, and asset serving. |
| 86 | +* Add **arch tests** to enforce boundaries (forbidden dependencies, env usage, etc.). |
| 87 | +* Target: **90%+** statement coverage; PRs below fail. |
| 88 | + |
| 89 | +### Required test suites & example scenarios |
| 90 | + |
| 91 | +* **CaptureService (Unit)** |
| 92 | + |
| 93 | + * stores payload with metadata and deterministic key format |
| 94 | + * lists messages in **newest‑first** order (server‐side sort) |
| 95 | + * updates existing message; returns null for unknown key |
| 96 | + * clears all messages; purge older than N seconds |
| 97 | + * rejects invalid keys (assertions/exceptions) |
| 98 | +* **StoreManager (Unit)** |
| 99 | + |
| 100 | + * resolves default `file` driver |
| 101 | + * throws on unsupported driver |
| 102 | + * accepts **custom resolver** via config and returns custom store instance |
| 103 | +* **Storage\FileStorage (Unit)** |
| 104 | + |
| 105 | + * persists and retrieves JSON payloads |
| 106 | + * `keys()` filters by timestamp; `delete()` removes files |
| 107 | + * `update()` merges atomically without data loss |
| 108 | + * `clear()` wipes namespace safely |
| 109 | +* **Transport\InboxTransport (Unit)** |
| 110 | + |
| 111 | + * normalizes and saves message when `enabled=true` and sets `storedKey` |
| 112 | + * decorates underlying transport when provided |
| 113 | + * no‑op capture when disabled; still delegates if decorated |
| 114 | +* **Support\MessageNormalizer (Unit)** |
| 115 | + |
| 116 | + * converts `SentMessage` to canonical structure (from, to, cc, subject, date, text, html, attachments, raw) |
| 117 | + * handles edge cases: missing headers, non‑UTF8, multiple parts |
| 118 | +* **Http Controllers (Feature)** |
| 119 | + |
| 120 | + * `InboxController` returns paginated list and sorts newest‑first |
| 121 | + * `SendTestMailController` sends sample mail through inbox transport |
| 122 | + * `ClearInboxController` empties store and returns success |
| 123 | + * `SeenController` toggles `seen_at` and returns updated entity |
| 124 | + * `AssetController` serves versioned assets with correct headers |
| 125 | +* **Middleware (Feature)** |
| 126 | + |
| 127 | + * `AuthorizeInboxMiddleware` denies/permits based on config/closure/gate |
| 128 | +* **Service Provider & Command (Feature)** |
| 129 | + |
| 130 | + * `InboxServiceProvider` registers bindings, publishes assets/config/routes |
| 131 | + * `InstallCommand` publishes resources and prints next steps |
| 132 | +* **Config (Arch)** |
| 133 | + |
| 134 | + * test no `env()` usage outside `config/` |
| 135 | + * config keys exist; defaults are sensible; validation for required options |
| 136 | + |
| 137 | +> Copilot: when you add a **new file**, also scaffold **tests** (Unit or Feature) with realistic data and edge cases. Update `phpstan.neon`, `pint.json`, and CI as needed. |
| 138 | +
|
| 139 | +## Pull requests & commit conventions |
| 140 | + |
| 141 | +* PRs should be **small and focused**, include tests, docs, and passing CI. |
| 142 | +* Use Conventional Commits (`feat:`, `fix:`, `chore:`, `test:`, `refactor:`, `docs:`). |
| 143 | +* Include a short description, screenshots (for UI), and checklists: tests added, coverage holds, docs updated. |
| 144 | + |
| 145 | +## Copilot do’s & don’ts |
| 146 | + |
| 147 | +* **Do**: follow these instructions for every generation, add missing tests, propose refactors that reduce complexity, prefer pure functions, suggest interfaces and DI. |
| 148 | +* **Do**: emit Pest tests using dataset‑driven cases, fakes, and Laravel helpers; prefer named routes in tests. |
| 149 | +* **Do**: add type‑safe DTOs and enums over associative arrays when evolving structures. |
| 150 | +* **Don’t**: introduce global state, call `env()` outside config, or mix UI concerns in controllers. |
| 151 | +* **Don’t**: add external services or self‑hosted SMTP; this package captures locally only. |
| 152 | + |
| 153 | +## Example prompts (for maintainers) |
| 154 | + |
| 155 | +* “Add newest‑first sorting to `CaptureService::all()` and cover with unit & feature tests.” |
| 156 | +* “Implement a `S3Storage` driver behind the `MessageStore` contract with contract tests mirrored from `FileStorage`.” |
| 157 | +* “Generate Vue components for Inbox list with read/unread states; |
| 158 | +* “Write an arch test preventing `env()` usage outside `config/` and preventing `Http\Controllers` from depending on `Storage\*` directly.” |
| 159 | + |
| 160 | +## Documentation expectations |
| 161 | + |
| 162 | +* Every public class/method should have minimal PHPDoc where types are not self‑evident. |
| 163 | +* Update `README.md` with any new features, configuration flags, or artisan commands. |
| 164 | +* Add upgrade notes on breaking changes under `CHANGELOG.md`. |
| 165 | + |
| 166 | +## CI expectations |
| 167 | + |
| 168 | +* GitHub Actions runs: install PHP/Node, cache deps, **run Pint, PHPStan, Pest with coverage**, and build assets. |
| 169 | +* PRs must pass all checks and maintain coverage thresholds. |
| 170 | + |
| 171 | +## Definition of done (per task) |
| 172 | + |
| 173 | +1. Code adheres to these standards |
| 174 | +2. Tests added and passing locally and in CI |
| 175 | +3. Static analysis & style pass |
| 176 | +4. Docs updated (README/CHANGELOG) |
| 177 | +5. No new global state, no env leaks |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +By keeping this document concise but explicit, Copilot can reliably scaffold code, tests, and reviews that align with this package’s architecture and quality bar. |
0 commit comments