# Developer guide ## Layout | Path | What | |------------------------------|-------------------------------------------------------------| | `backend/src/s365b/core` | Config, logging, secrets, catalog, backup engine, events, services | | `backend/src/s365b/storage` | Repository format and storage backends | | `backend/src/s365b/graph` | Microsoft Graph client and certificate auth | | `backend/src/s365b/modules` | Module SDK (`sdk.py`), registry, contract checks; one sub-package per module | | `backend/src/s365b/db` | SQLAlchemy models, sessions, packaged Alembic migrations | | `backend/src/s365b/jobs` | Job runner and Procrastinate tasks (incl. scheduler tick) | | `backend/src/s365b/api` | FastAPI routes; also serves the built web UI | | `backend/src/s365b/auth` | Passwords, TOTP, sessions, break-glass and Entra sign-in | | `backend/src/s365b/cli.py` | The `s365b` command (`api`, `worker`, `migrate`, …) | | `frontend/src` | Web UI: React + TypeScript (`pages/`, `components/`, `lib/`) | | `frontend/e2e` | Browser smoke test (also takes screenshots) | | `Dockerfile` | One image: builds the UI (Node), installs the backend | | `deploy/` | Compose stack, Caddyfile, `init.sh` | | `docs/` | This site (Sphinx + MyST) | The {doc}`API reference ` is generated from the source docstrings. ## Set up ```bash cd backend python3.13 -m venv .venv .venv/bin/pip install -e '.[dev]' ``` ## Quality gates CI runs all of these; run them before committing: ```bash .venv/bin/ruff check src tests && .venv/bin/ruff format --check src tests .venv/bin/mypy src # strict mode .venv/bin/interrogate src # 100% docstrings on public code .venv/bin/pytest -q ``` Docstrings use the Google style (`Args:`, `Returns:`, `Raises:`), enforced by ruff's pydocstyle rules. ## Web UI ```bash cd frontend npm ci npm run dev # http://localhost:5173, proxies /api to S365B_DEV_API (default https://localhost:8443) npm run build # type-check + production build into dist/ npm test # unit tests (vitest) ``` Conventions: - **Platform first.** Native `` for modals, `popover` for toasts, `` for switches, `:user-invalid` for field errors, `light-dark()` colour tokens. Polyfills load only where a feature is missing (`src/lib/polyfills.ts`). - **No inline scripts or styles in markup.** The API serves the UI with a strict Content-Security-Policy (`script-src 'self'; style-src 'self'`); runtime values such as brand colours are set as CSS custom properties. - **Branding** arrives from `GET /api/v1/branding` before the first render (`src/lib/theme.ts`). Text on brand colours uses CSS `contrast-color()` with a computed fallback (`src/lib/color.ts`). - **Forms:** visible labels, hints above inputs, errors linked through `data-error` (`src/lib/formA11y.ts` keeps `aria-invalid` and `aria-describedby` in step). - **API calls** go through `src/lib/api.ts`, which sends the CSRF header. - **Text** goes through `t()` (see below); dates and numbers through `Intl` formatters created with `locale()`. ### Translations Every user-visible string is a message: an id and its English text, both literals, in ICU MessageFormat. ```tsx import { t, tx } from "../i18n"; t("jobs.queued", "{job} queued.", { job: job.name }); t("alerts.items", "{n, plural, one {# item} other {# items}}", { n }); tx("jobs.noRepo.admin", "Backups need somewhere to go. Set up backup storage first.", { link: (c) => {c}, }); ``` - Write whole sentences with placeholders. Never build a sentence from pieces or add an English plural `s`: word order and plurals differ. - Text from Microsoft 365 or the server is data, not a message. Mark server-provided UI text (such as module descriptions) with `data-source="server"` until the server sends message codes. - `npm run i18n` extracts the messages into `src/locales/en.json` (the build, dev server and tests run it first; CI fails if the committed file is out of date). It also writes the pseudo-language `en-XA`: open the UI with `?lang=en-XA` and anything not accented and in brackets was missed. - Every other catalog (`es.json`, `fr.json`) must have exactly the English ids, the same ``, and no placeholders English lacks: `src/locales/catalog.test.ts` checks this, so a new string needs its translations in the same change. Use ’ rather than `'`, which ICU treats as an escape. French uses a no-break space before `:` and a narrow one before `; ? !`. - To add a language: add its catalog, its entry in `LANGUAGES` and `load()` in `src/i18n/index.ts`, add it to `BETA_LANGUAGES` until a native speaker has reviewed it, and to `default_language` in `backend/src/s365b/core/branding.py`. Follow Microsoft's terminology for that language (the Microsoft Terminology collection), so the words match the Microsoft 365 admin centre people already know. ### Browser smoke test `npm run e2e` drives Chrome through first-run setup, enrolment, sign-in and every page, checks every page in the pseudo-language for untranslated text, and saves screenshots to `frontend/e2e/shots/` (including French ones, for checking layout with longer text). It needs an API serving the built UI against an **empty** database: ```bash export S365B_DATABASE_URL=postgresql+psycopg://… # disposable database export S365B_MASTER_KEY_FILE=/tmp/s365b-master-key # s365b generate-master-key > … export S365B_WEB_DIR=$PWD/frontend/dist S365B_BASE_URL=http://localhost:8765 backend/.venv/bin/s365b migrate backend/.venv/bin/s365b api --host 127.0.0.1 --port 8765 & (cd frontend && npm run build && npm run e2e) ``` ## Third-party software The About page lists the open-source software Mail Safehouse ships with. The server lists its own Python packages from what is installed (`s365b.core.third_party`). The web app's packages and the containers come from `frontend/src/generated/third-party.json`; after changing the web app's dependencies (or `LICENSE`/`NOTICE`), run ```bash python3 scripts/third_party.py ``` It also writes `frontend/public/third-party-licenses.txt` (the full licence texts of the bundled packages) and copies `LICENSE` and `NOTICE` beside the web app. A frontend test fails while these are out of date. ## Tests that need PostgreSQL Tests marked `postgres` (catalog, engine, jobs, API) run only when `S365B_TEST_DATABASE_URL` points to a **disposable** database: the schema is dropped and recreated for every test. ```bash docker run -d --name s365b-testdb -e POSTGRES_USER=s365b -e POSTGRES_PASSWORD=test \ -e POSTGRES_DB=s365b -p 127.0.0.1:55432:5432 docker.io/library/postgres:17 export S365B_TEST_DATABASE_URL=postgresql+psycopg://s365b:test@127.0.0.1:55432/s365b .venv/bin/pytest -q ``` ## Database changes Edit `s365b/db/models.py`, then generate a migration against a database at the current head: ```bash S365B_DATABASE_URL=... .venv/bin/python -c \ "from s365b.db.migrate import autogenerate; autogenerate('describe the change')" ``` Review the generated file in `s365b/db/migrations/versions/` (Alembic does not drop enum types in downgrades, for example). CI fails if the models and migrations disagree. ## Writing a module 1. Create `s365b/modules//` with a subclass of {py:class}`s365b.modules.sdk.BackupModule`. 2. Register it in `pyproject.toml`: ```toml [project.entry-points."s365b.modules"] entra = "s365b.modules.entra:EntraModule" ``` 3. In `backup()`, yield {py:class}`~s365b.modules.sdk.ItemVersion` records and a {py:class}`~s365b.modules.sdk.Checkpoint` after each unit of work (a folder, a delta page). The engine commits at checkpoints and resumes from the last one after a failure. 4. Call `assert_module_contract(YourModule)` from {py:mod}`s365b.modules.testing` in your tests, and test `backup()` against recorded Graph responses with `respx`. 5. Add a page for the module to the docs. Backup permissions must be read-only; the contract check enforces it.