Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

docs: document the workspaces setup and test folders

spacesdrive5fa60cf7154c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5fc8de1..1ca2535 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,8 +34,17 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Charts use the brand data-visualisation series in both themes
- Settings, Scheduled publishing now says who needs to set anything up: members nothing, administrators only the backend deploy. The GitHub backup guide is marked optional and for administrators, and starts folded
+**Development setup**
+- One `npm install` at the repository root installs the backend and the frontend as npm workspaces, with a single root `package-lock.json`; never install inside `backend/` or `frontend/`
+- `npm run dev` starts the Worker (port 8787) and the Vite dev server (port 5173) together; `npm run build`, `npm test`, `npm run lint` and `npm run format` also run from the root
+- One ESLint config, Prettier config and EditorConfig at the root; the backend is now linted too, and the invisible BOM characters in its regexes are written as `\uFEFF` escapes
+- Tests moved out of the source folders into `backend/tests/` and `frontend/tests/`, mirroring the source tree; backend fixtures and golden snapshots are in `backend/tests/fixtures/`
+- The deploy workflows install once at the root on Node 24, deploy with the Wrangler version from the lockfile, and also run when the root `package.json` or `package-lock.json` changes
+- Removed the unused Express packages from the root `package.json` and the leftover `backend/nodemon.json`
+
### Fixed
+- The frontend test for the Google sign-in nonce failed about one run in 23, because it required at least 40 characters while the nonce length varies around 36 to 43
- The Worker cron and the GitHub Actions workflow could both publish the same scheduled post; posts are now claimed atomically
- Channels with no public videos failed to load with "playlist cannot be found"; they now open with a "No public videos yet" message
- Overview could show "Syncing analytics" forever: Reddit accounts and empty channels never finished syncing, and the page only re-checked once
diff --git a/DECISIONS.md b/DECISIONS.md
index 8a41745..a776062 100644
--- a/DECISIONS.md
+++ b/DECISIONS.md
@@ -590,3 +590,20 @@ Records every significant architectural decision, the alternatives considered, a
- The button is rendered by Google in an iframe, so its styling is limited to Google's options (theme follows the app theme).
- Self-hosters on `pages.dev` can still use it, but cannot get brand verification until they add a custom domain.
+---
+
+## ADR-028: npm workspaces monorepo with shared root tooling and dedicated test folders
+
+**Date:** 2026-10-14
+**Status:** Accepted
+
+**Decision:** The repository root is an npm workspaces root (`"workspaces": ["backend", "frontend"]`, private). One `npm install` at the root installs both apps into a single `node_modules`, from a single root `package-lock.json`; the lockfiles inside `backend/` and `frontend/` are deleted. Root scripts cover the whole repository: `npm run dev` starts `wrangler dev` (port 8787) and Vite (port 5173) together with `concurrently`, and `npm run build`, `npm test`, `npm run lint` and `npm run format` run from the root. Each app keeps its own dependencies in its own `package.json`, added with `npm install <pkg> --workspace backend|frontend`; the root `package.json` holds only shared dev tooling (ESLint, `@eslint/js`, the React hooks and React Refresh plugins, `globals`, Prettier, `concurrently`). One ESLint flat config (`eslint.config.mjs`), one Prettier config (`.prettierrc.json`, matching the existing style) and one `.editorconfig` live at the root, and the backend is now linted with the recommended rules and service worker globals. Tests move out of the source folders into `backend/tests/` and `frontend/tests/`, which mirror the source tree, with backend fixtures and golden snapshots in `backend/tests/fixtures/`. The deploy workflows run `npm ci` once at the root on Node 24 and deploy with the Wrangler version pinned in the lockfile.
+
+**Context:** The root held an unused Express package from the original prototype, so each app had to be installed and started separately in two terminals, with two lockfiles that could drift. ESLint only covered the frontend, so the Worker code was never linted, and there was no shared formatting config. Test files and golden fixtures sat beside the Worker and React source, mixing code that ships with code that does not. `backend/nodemon.json` was left over from the Express setup; `wrangler dev` already reloads on save.
+
+**Trade-offs:**
+- A dependency change always touches the root lockfile, and both deploy workflows run when it changes, even if only one app's dependencies moved.
+- Running `npm install` inside `backend/` or `frontend/` by mistake can create a stray lockfile there; the docs and the pre-commit checklist call this out.
+- Shared packages are hoisted to the root `node_modules`, so a script can resolve a package its own workspace does not declare. Declaring dependencies in the workspace that uses them stays a review rule, not something npm enforces.
+- Prettier is configured but the existing code was not reformatted in bulk, to keep history readable; nothing enforces formatting until someone runs `npm run format` in its own commit.
+- Tests import source through longer relative paths (`../../services/...`) in exchange for a source tree that holds only code that ships.
diff --git a/README.md b/README.md
index d4f66b2..4e176d5 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,7 @@ You deploy one Cloudflare Worker and one Cloudflare Pages site using your own AP
- [Quick Start](#quick-start)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
+- [Development](#development)
- [Configuration](#configuration)
- [Scheduled Publishing with GitHub Actions](#scheduled-publishing-with-github-actions)
- [How It Works](#how-it-works)
@@ -126,24 +127,22 @@ You deploy one Cloudflare Worker and one Cloudflare Pages site using your own AP
## Quick Start
-Clone and install dependencies:
+Clone and install dependencies (one install at the root covers the backend and the frontend):
```bash
git clone https://github.com/spacesdrive/twiligent.git
-cd twiligent/backend && npm install
-cd ../frontend && npm install
+cd twiligent
+npm install
```
-Create `backend/.dev.vars` with your credentials (see [Configuration](#configuration)), then start both servers:
+Create `backend/.dev.vars` with your credentials (see [Configuration](#configuration)), then start both servers from the root:
```bash
-# Terminal 1 - backend (port 8787)
-cd backend && wrangler dev
-
-# Terminal 2 - frontend (port 5173)
-cd frontend && npm run dev
+npm run dev
```
+This runs the backend Worker on port 8787 and the frontend on port 5173 in one terminal.
+
Open [http://localhost:5173](http://localhost:5173) and sign up for an account, then go to **Accounts** to add your first YouTube channel or Instagram account.
The full documentation, including a step-by-step self-hosting guide that explains every key, is built into the app at `/docs` (for example [http://localhost:5173/docs](http://localhost:5173/docs)). It is public, so you can share the link with members before they sign in.
@@ -156,8 +155,8 @@ The full documentation, including a step-by-step self-hosting guide that explain
| Requirement | Notes |
|---|---|
-| Node.js 20 | Required for Wrangler CLI |
-| Wrangler CLI | `npm install -g wrangler`, then `wrangler login` |
+| Node.js 22 or later | Required by the dev tooling (Vite 7, concurrently); the deploy workflows use Node 24 |
+| Wrangler CLI | Installed by `npm install` at the root; run `npx wrangler login` once |
| Cloudflare account | Free tier sufficient for Workers and Pages |
| Supabase project | Free tier sufficient; create at [supabase.com](https://supabase.com) |
| YouTube Data API v3 key | Required for YouTube analytics |
@@ -178,10 +177,11 @@ cd twiligent
**2. Install dependencies**
```bash
-cd backend && npm install
-cd ../frontend && npm install
+npm install
```
+The repository is an npm workspaces monorepo: `backend/` and `frontend/` are workspaces, and one install at the root fills a single `node_modules` from the single root `package-lock.json`. Do not run `npm install` inside `backend/` or `frontend/`.
+
**3. Create the Supabase database tables**
In your Supabase project, open the SQL editor and run the following:
@@ -244,14 +244,32 @@ See [Configuration](#configuration) below.
**5. Start the development servers**
```bash
-# Terminal 1
-cd backend && wrangler dev
-
-# Terminal 2
-cd frontend && npm run dev
+npm run dev
```
-The backend runs on port 8787. The frontend runs on port 5173. Open [http://localhost:5173](http://localhost:5173).
+The backend runs on port 8787. The frontend runs on port 5173. Open [http://localhost:5173](http://localhost:5173). To run one side alone, use `npm run dev:backend` or `npm run dev:frontend`.
+
+---
+
+## Development
+
+Run every command from the repository root.
+
+| Command | What it does |
+|---|---|
+| `npm install` | Installs both workspaces and the shared tooling |
+| `npm run dev` | Starts the Worker (`wrangler dev`, port 8787) and Vite (port 5173) together |
+| `npm run dev:backend` / `npm run dev:frontend` | Starts one of them |
+| `npm run build` | Builds the frontend into `frontend/dist/` |
+| `npm test` | Runs the backend tests, then the frontend tests (Node's built-in test runner) |
+| `npm run lint` | Runs ESLint over the whole repository with the shared `eslint.config.mjs` |
+| `npm run format` | Applies Prettier (`.prettierrc.json`) to the whole repository |
+
+Add a dependency to the app that uses it: `npm install <package> --workspace backend` or `npm install <package> --workspace frontend`. Only shared development tooling goes in the root `package.json`.
+
+Tests live outside the source folders, in `backend/tests/` and `frontend/tests/`, and mirror the source tree: the test for `backend/services/youtube.js` is `backend/tests/services/youtube.test.js`, and the test for `frontend/src/lib/accounts.js` is `frontend/tests/lib/accounts.test.js`. Backend fixtures and golden snapshots are in `backend/tests/fixtures/`.
+
+ESLint, Prettier and EditorConfig are configured once at the root. The existing code has not been reformatted in bulk, so run `npm run format` only when you mean to reformat.
---
@@ -292,21 +310,21 @@ VITE_GOOGLE_CLIENT_ID=1234567890-abc.apps.googleusercontent.com
### Production (Cloudflare Workers secrets)
-Set each secret via Wrangler:
+Set each secret via Wrangler, from the `backend/` folder:
```bash
-wrangler secret put SUPABASE_URL
-wrangler secret put SUPABASE_SERVICE_KEY
-wrangler secret put SUPABASE_ANON_KEY
-wrangler secret put YOUTUBE_API_KEY
-wrangler secret put INSTAGRAM_APP_ID
-wrangler secret put INSTAGRAM_APP_SECRET
-wrangler secret put CLOUDINARY_CLOUD_NAME
-wrangler secret put CLOUDINARY_UPLOAD_PRESET
-wrangler secret put BACKEND_URL
-wrangler secret put FRONTEND_URL
-wrangler secret put UPSTASH_REDIS_REST_URL
-wrangler secret put UPSTASH_REDIS_REST_TOKEN
+npx wrangler secret put SUPABASE_URL
+npx wrangler secret put SUPABASE_SERVICE_KEY
+npx wrangler secret put SUPABASE_ANON_KEY
+npx wrangler secret put YOUTUBE_API_KEY
+npx wrangler secret put INSTAGRAM_APP_ID
+npx wrangler secret put INSTAGRAM_APP_SECRET
+npx wrangler secret put CLOUDINARY_CLOUD_NAME
+npx wrangler secret put CLOUDINARY_UPLOAD_PRESET
+npx wrangler secret put BACKEND_URL
+npx wrangler secret put FRONTEND_URL
+npx wrangler secret put UPSTASH_REDIS_REST_URL
+npx wrangler secret put UPSTASH_REDIS_REST_TOKEN
```
### Getting API credentials
@@ -409,8 +427,8 @@ flowchart TD
twiligent/
├── .github/
│ └── workflows/
-│ ├── deploy-backend.yml # Wrangler deploy on backend changes
-│ ├── deploy-frontend.yml # Cloudflare Pages deploy on frontend changes
+│ ├── deploy-backend.yml # Wrangler deploy on backend or root lockfile changes
+│ ├── deploy-frontend.yml # Cloudflare Pages deploy on frontend or root lockfile changes
│ └── publish-scheduled.yml # Scheduled post fallback (every 15 minutes)
│
├── backend/
@@ -432,10 +450,14 @@ twiligent/
│ ├── services/
│ │ ├── youtube.js # YouTube Data API v3 client
│ │ └── instagram.js # Instagram Graph API client
+│ ├── tests/ # Node tests mirroring lib/, routes/, services/, utils/; fixtures/ holds fakes and golden snapshots
│ ├── server.js # Hono app + Worker export (fetch + scheduled)
-│ └── wrangler.toml # Cloudflare Worker config and cron triggers
+│ ├── wrangler.toml # Cloudflare Worker config and cron triggers
+│ └── package.json # Backend workspace: hono, supabase-js, upstash redis, wrangler
│
├── frontend/
+│ ├── tests/ # Node tests mirroring src/ (lib, utils, features, docs)
+│ ├── package.json # Frontend workspace: React, Vite, Tailwind, shadcn
│ └── src/
│ ├── features/
│ │ ├── analytics/
@@ -457,8 +479,14 @@ twiligent/
│ └── services/
│ └── api.js # Frontend API client (all fetch calls)
│
-└── scripts/
- └── publish-scheduled.js # Node.js script run by GitHub Actions fallback
+├── scripts/
+│ └── publish-scheduled.js # Node.js script run by GitHub Actions fallback (no dependencies)
+│
+├── package.json # npm workspaces root: scripts and shared dev tooling
+├── package-lock.json # The only lockfile, for both workspaces
+├── eslint.config.mjs # Shared ESLint config
+├── .prettierrc.json # Shared Prettier config
+└── .editorconfig # Shared editor defaults
```
---
@@ -532,7 +560,7 @@ Contributions are welcome. For significant changes, open an issue first to discu
git checkout -b feature/your-feature-name
```
-3. Make your changes and commit
+3. Make your changes, check them from the repository root with `npm run build`, `npm test` and `npm run lint`, and commit
```bash
git commit -m "feat: add your feature description"
diff --git a/ROADMAP.md b/ROADMAP.md
index a44f236..6f52e98 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -126,8 +126,8 @@ React Native app using the same Cloudflare Worker backend.
## Known Issues / Tech Debt
- ~~Redis cache TTL is never set~~ Done: analytics caches expire after 6 hours.
-- `public/` directory at repo root contains a legacy Express prototype - should be removed once it is confirmed unused.
-- Root `package.json` and `node_modules/` belong to the legacy prototype - can be removed.
+- ~~`public/` directory at repo root contains a legacy Express prototype~~ Done: the legacy root dashboard was removed.
+- ~~Root `package.json` and `node_modules/` belong to the legacy prototype~~ Done: the root `package.json` is now the npm workspaces root for `backend/` and `frontend/` with shared dev tooling, and the Express prototype dependencies are gone.
- `wrangler.toml` has no `vars` section for non-secret configuration - any non-sensitive config currently requires a redeploy to change.
- No error monitoring (Sentry, Cloudflare Analytics, etc.) - failures are only visible in Cloudflare Worker logs.
- The `scripts/publish-scheduled.js` scheduler has no rate limiting - if 100 posts are due at the same time, it will attempt to publish all 100 concurrently within a single GitHub Actions run.
diff --git a/claude.md b/claude.md
index 87936f9..52b6365 100644
--- a/claude.md
+++ b/claude.md
@@ -11,6 +11,28 @@ Twiligent is a self-hosted social media analytics and publishing dashboard for c
---
+## Repository Layout
+
+The repo is an npm workspaces monorepo. Run every install and script from the root.
+
+```
+package.json workspaces ["backend", "frontend"], shared dev tooling, root scripts
+package-lock.json the only lockfile; one node_modules at the root
+eslint.config.mjs shared ESLint config (frontend rules, backend Workers rules, Node rules for tests)
+.prettierrc.json shared Prettier config matching the existing style (not applied in bulk)
+.editorconfig editor defaults (4 spaces in backend, 2 elsewhere)
+backend/ Hono Worker; tests in backend/tests/ mirroring the source folders
+frontend/ React SPA; tests in frontend/tests/ mirroring frontend/src/
+scripts/ publish-scheduled.js, run by GitHub Actions with no dependencies
+```
+
+- `npm install` at the root installs both apps. Never run `npm install` inside `backend/` or `frontend/`, and never commit a lockfile there.
+- `npm run dev` starts the Worker (port 8787) and Vite (port 5173) together; `npm test`, `npm run lint` and `npm run build` cover the whole repo.
+- Add a dependency to the app that uses it: `npm install <pkg> --workspace backend` or `--workspace frontend`. Only shared dev tooling belongs in the root `package.json`.
+- Tests never live next to source files. A test for `backend/services/youtube.js` is `backend/tests/services/youtube.test.js`; a test for `frontend/src/lib/accounts.js` is `frontend/tests/lib/accounts.test.js`.
+
+---
+
## Reading Order Before Any Change
Before implementing any feature or fix, read these documents in this exact order:
@@ -275,10 +297,15 @@ When anything changes, update every relevant docs file from the table below. The
| Env var removed | `docs/architecture/cloudflare/WORKERS.md`, `docs/architecture/cloudflare/CI_CD.md`, `docs/workflows/DEPLOYMENT.md` |
| New npm package (backend) | `docs/architecture/backend/HONO.md`, `DECISIONS.md` |
| New npm package (frontend) | `docs/architecture/frontend/REACT_ARCHITECTURE.md`, `DECISIONS.md` |
+| New npm package (root dev tooling) | `docs/workflows/TESTING.md` or the guideline it serves, `DECISIONS.md`, `README.md` |
+| Workspaces, root `package.json` scripts or the root lockfile changed | `README.md`, `docs/workflows/DEPLOYMENT.md`, `docs/workflows/FEATURE_DEVELOPMENT.md`, `docs/architecture/cloudflare/CI_CD.md`, this file (Repository Layout section) |
+| Shared tooling config changed (`eslint.config.mjs`, `.prettierrc.json`, `.editorconfig`) | `docs/guidelines/JAVASCRIPT.md`, `docs/workflows/TESTING.md`, this file (Repository Layout section) |
| New frontend feature module | `docs/architecture/frontend/FEATURES.md`, `docs/architecture/frontend/ROUTING.md`, `docs/architecture/OVERVIEW.md` |
| New data flow | `docs/architecture/data-flows/` (add or update relevant file), `docs/architecture/OVERVIEW.md` |
| New cron trigger | `docs/architecture/cloudflare/WORKERS.md`, `docs/architecture/backend/CRON.md`, `docs/architecture/cloudflare/CI_CD.md` |
| New GitHub Actions workflow | `docs/architecture/cloudflare/CI_CD.md`, `docs/workflows/DEPLOYMENT.md` |
+| Existing GitHub Actions workflow changed (steps, triggers, Node version) | `docs/architecture/cloudflare/CI_CD.md`, `docs/workflows/DEPLOYMENT.md`, `docs/architecture/cloudflare/PAGES.md` or `WORKERS.md` for the deploy it runs |
+| Test added, moved or test command changed | `docs/workflows/TESTING.md` (layout, commands, coverage list) |
| New backend service file | `docs/architecture/backend/SERVICES.md` |
| New cache key | `docs/architecture/backend/CACHING.md` |
| New middleware | `docs/architecture/backend/MIDDLEWARE.md`, `docs/architecture/backend/HONO.md` |
@@ -304,4 +331,4 @@ When anything changes, update every relevant docs file from the table below. The
| Workflow changed | `docs/workflows/` (relevant file) |
| Guideline changed | `docs/guidelines/` (relevant file) |
| Philosophy changed | `docs/philosophy/` (relevant file) |
-| Project setup, features, or deployment instructions change | `README.md` |
+| Project setup, features, or deployment instructions change | `README.md`, and the self-hosting pages in `frontend/src/docs/pages/selfHost/` |
diff --git a/docs/architecture/backend/CRON.md b/docs/architecture/backend/CRON.md
index 5b7499d..1748a98 100644
--- a/docs/architecture/backend/CRON.md
+++ b/docs/architecture/backend/CRON.md
@@ -24,7 +24,7 @@ Both cron triggers invoke the `scheduled` export from `backend/server.js`.
- On success: sets status to `'published'`, stores `publishedMediaId` and `publishedAt` in `data`
- On failure: sets status to `'failed'`, stores `error` message in `data`
-`utils/scheduler.test.js` pins this sequence, the log lines and the stored results against a fake Supabase client and Graph API.
+`backend/tests/utils/scheduler.test.js` pins this sequence, the log lines and the stored results against a fake Supabase client and Graph API (fixtures and golden snapshots in `backend/tests/fixtures/`).
**No userId filter** - the scheduler operates across all users. It uses the service-role Supabase client.
diff --git a/docs/architecture/backend/HONO.md b/docs/architecture/backend/HONO.md
index 1ef652d..97e1dcb 100644
--- a/docs/architecture/backend/HONO.md
+++ b/docs/architecture/backend/HONO.md
@@ -134,3 +134,21 @@ async scheduled(event, env, ctx) {
```
**Never hardcode secrets.** All secrets must come from `c.env` (Worker secrets set via `wrangler secret put`).
+
+## Packages and Tooling
+
+`backend/` is the `twiligent-backend` workspace of the root npm workspaces monorepo. Its `package.json` lists only what the Worker needs:
+
+| Package | Kind | Purpose |
+|---|---|---|
+| `hono` | runtime | Routing and middleware |
+| `@supabase/supabase-js` | runtime | Database and JWT verification |
+| `@upstash/redis` | runtime | Optional cache over REST |
+| `wrangler` | dev | Local dev server, deploys and secrets (version pinned by the root lockfile) |
+
+- Install from the repository root with `npm install`; it fills the single root `node_modules`. Add a backend package with `npm install <pkg> --workspace backend` (new runtime packages need explicit approval, see CLAUDE.md). There is no `backend/package-lock.json`.
+- Workspace scripts: `dev` and `start` run `wrangler dev`, `deploy` runs `wrangler deploy`, `test` runs `node --test "tests/**/*.test.js"`. From the root, `npm run dev:backend` starts the Worker alone, `npm run dev` starts it with Vite, and `npm test` runs the backend tests before the frontend ones.
+- `wrangler dev` reloads on save, so there is no nodemon or other watcher.
+- Lint is the root `eslint.config.mjs`: backend source gets the ESLint recommended rules with service worker globals (the Worker runs on the web platform, not Node), and `backend/tests/` gets Node globals. Run `npm run lint` from the root.
+- Formatting follows the root `.prettierrc.json` and `.editorconfig`: 4-space indentation in `backend/`, single quotes, semicolons, 120 columns.
+- Tests and fixtures live in `backend/tests/`, outside the source folders, so Wrangler never bundles them. See `docs/workflows/TESTING.md`.
diff --git a/docs/architecture/cloudflare/CI_CD.md b/docs/architecture/cloudflare/CI_CD.md
index a16daee..f64edc5 100644
--- a/docs/architecture/cloudflare/CI_CD.md
+++ b/docs/architecture/cloudflare/CI_CD.md
@@ -4,11 +4,13 @@
### `deploy-backend.yml`
-**Trigger:** Push to `main` branch, when files under `backend/**` or `.github/workflows/deploy-backend.yml` change
+**Trigger:** Push to `main` branch, when files under `backend/**`, the root `package.json`, the root `package-lock.json` or `.github/workflows/deploy-backend.yml` change; also `workflow_dispatch` for a manual run
**What it does:**
1. Checkout repo
-2. Run `wrangler deploy` via `cloudflare/wrangler-action@v4`
+2. `actions/setup-node@v4` with Node 24 and the npm cache keyed on the root `package-lock.json`
+3. `npm ci` at the repository root, which installs every workspace, including Wrangler
+4. Run `wrangler deploy` via `cloudflare/wrangler-action@v4` with `workingDirectory: backend`. The action finds the Wrangler installed from the lockfile (3.114.17) and uses it instead of installing its own default version.
**Required GitHub secrets:**
- `CLOUDFLARE_API_TOKEN` - API token with Worker deploy permission
@@ -20,13 +22,14 @@
### `deploy-frontend.yml`
-**Trigger:** Push to `main` branch, when files under `frontend/**` or `.github/workflows/deploy-frontend.yml` change
+**Trigger:** Push to `main` branch, when files under `frontend/**`, the root `package.json`, the root `package-lock.json` or `.github/workflows/deploy-frontend.yml` change; also `workflow_dispatch` for a manual run
**What it does:**
1. Checkout repo
-2. `npm ci` (in `frontend/`)
-3. `npm run build` - Vite builds to `frontend/dist/`
-4. Deploy `dist/` to Cloudflare Pages via `wrangler pages deploy`
+2. `actions/setup-node@v4` with Node 24 and the npm cache keyed on the root `package-lock.json`
+3. `npm ci` at the repository root, which installs every workspace
+4. `npm run build --workspace frontend` - Vite builds to `frontend/dist/`
+5. Deploy `dist/` to Cloudflare Pages via `wrangler pages deploy` (`cloudflare/wrangler-action@v4` with `workingDirectory: frontend`, using the Wrangler from the lockfile)
**Required GitHub secrets:**
- `CLOUDFLARE_API_TOKEN`
@@ -65,6 +68,8 @@
GitHub Actions path filters mean:
- Editing `frontend/` only -> triggers `deploy-frontend.yml`, not `deploy-backend.yml`
- Editing `backend/` only -> triggers `deploy-backend.yml`, not `deploy-frontend.yml`
+- Editing the root `package.json` or `package-lock.json` -> triggers both, because the single lockfile pins the dependencies of both apps
+- Editing root tooling config (`eslint.config.mjs`, `.prettierrc.json`, `.editorconfig`) -> triggers nothing; no workflow runs lint or tests
- Editing `scripts/` -> triggers neither deploy workflow (only `publish-scheduled.yml` on its cron)
- Editing `docs/` -> triggers nothing
- Editing `CLAUDE.md`, `DECISIONS.md`, etc. -> triggers nothing
@@ -89,5 +94,6 @@ Trigger `publish-scheduled.yml` manually via GitHub Actions UI -> Actions -> "In
1. Create `.github/workflows/my-workflow.yml`
2. Add appropriate path filters to avoid unnecessary runs
-3. Document it in `docs/architecture/cloudflare/CI_CD.md` (this file)
-4. Add any required secrets via GitHub repo -> Settings -> Secrets -> Actions
+3. If the workflow needs project dependencies, run `actions/setup-node` with `cache-dependency-path: package-lock.json` and `npm ci` at the repository root; there are no lockfiles inside `backend/` or `frontend/`
+4. Document it in `docs/architecture/cloudflare/CI_CD.md` (this file)
+5. Add any required secrets via GitHub repo -> Settings -> Secrets -> Actions
diff --git a/docs/architecture/cloudflare/PAGES.md b/docs/architecture/cloudflare/PAGES.md
index e0f4ab7..60552a9 100644
--- a/docs/architecture/cloudflare/PAGES.md
+++ b/docs/architecture/cloudflare/PAGES.md
@@ -9,12 +9,12 @@ The React frontend is deployed to Cloudflare Pages. It is a static SPA - all rou
| Setting | Value |
|---|---|
| Framework preset | None (Vite) |
-| Build command | `cd frontend && npm install && npm run build` |
+| Build command | `npm ci && npm run build` |
| Build output directory | `frontend/dist` |
| Root directory | `/` (repo root) |
-| Node.js version | 20 |
+| Node.js version | 22 or later (the deploy workflow uses 24; Vite 7 needs 20.19 or later) |
-These are configured in the Cloudflare Pages project settings, not in a file.
+These are configured in the Cloudflare Pages project settings, not in a file. The repository is an npm workspaces monorepo: the install runs once at the root against the root `package-lock.json` (there is no lockfile in `frontend/`), and the root `build` script runs `npm run build --workspace frontend`.
## SPA Fallback
@@ -48,14 +48,14 @@ Vite exposes them at runtime as `import.meta.env.VITE_*`.
## Deployment
-Cloudflare Pages deploys automatically on every push to `main` that changes files in `frontend/`. This is configured in `.github/workflows/deploy-frontend.yml`.
+Cloudflare Pages deploys automatically on every push to `main` that changes files in `frontend/`, the root `package.json` or the root `package-lock.json`. This is configured in `.github/workflows/deploy-frontend.yml`, which runs `npm ci` at the root, builds with `npm run build --workspace frontend` and uploads `frontend/dist` with the Wrangler version from the lockfile.
-Manual deployment (emergency or first setup):
+Manual deployment (emergency or first setup), from the repository root:
```bash
-cd frontend
-npm install
+npm ci
npm run build
+cd frontend
npx wrangler pages deploy dist --project-name=twiligent
```
@@ -67,16 +67,14 @@ The app is accessible at both:
## Local Development
-```bash
-cd frontend && npm run dev
-```
-
-The frontend dev server proxies `/api` requests to the Worker (see `frontend/vite.config.js`). Start the backend Worker at the same time:
+From the repository root, start the Vite dev server (port 5173) and the backend Worker (port 8787) together:
```bash
-cd backend && wrangler dev
+npm run dev
```
+The frontend calls the Worker at `VITE_API_URL`, which defaults to `http://localhost:8787/api` when unset (see `frontend/src/services/api.js`); Vite does not proxy requests. To run the frontend alone, use `npm run dev:frontend`.
+
## First-Time Setup
1. Create a new Cloudflare Pages project in the Cloudflare dashboard
diff --git a/docs/architecture/cloudflare/WORKERS.md b/docs/architecture/cloudflare/WORKERS.md
index 44b7a51..8da4f1e 100644
--- a/docs/architecture/cloudflare/WORKERS.md
+++ b/docs/architecture/cloudflare/WORKERS.md
@@ -77,7 +77,7 @@ UPSTASH_REDIS_REST_URL=https://...
UPSTASH_REDIS_REST_TOKEN=...
```
-Run locally: `cd backend && wrangler dev`
+Run locally from the repository root: `npm run dev:backend` (Worker only, port 8787) or `npm run dev` (Worker and Vite together). Both run `wrangler dev` in `backend/`, which reloads on save.
## Worker Constraints
diff --git a/docs/architecture/frontend/DESIGN_SYSTEM.md b/docs/architecture/frontend/DESIGN_SYSTEM.md
index 73f2f0e..10c5087 100644
--- a/docs/architecture/frontend/DESIGN_SYSTEM.md
+++ b/docs/architecture/frontend/DESIGN_SYSTEM.md
@@ -64,4 +64,4 @@ Shared components live in `frontend/src/components/`. Reach for these before wri
- Every async action disables its trigger, shows a spinner or progress, and ends in a toast that names what happened.
- Sentence case in all UI copy. No exclamation marks. Errors say what happened and what to do.
- Lucide icons at 1.75 stroke. 18px in navigation, 16px in controls.
-- New logic that can be pure goes in `src/lib/` with a `*.test.js` beside it (`npm test`).
+- New logic that can be pure goes in `src/lib/`, with its test at the mirrored path in `frontend/tests/` (`src/lib/library.js` -> `frontend/tests/lib/library.test.js`; run `npm test`).
diff --git a/docs/architecture/frontend/FEATURES.md b/docs/architecture/frontend/FEATURES.md
index 9c31497..416eba9 100644
--- a/docs/architecture/frontend/FEATURES.md
+++ b/docs/architecture/frontend/FEATURES.md
@@ -116,6 +116,8 @@ frontend/src/
│ └── reelsLibrary.js <- Pure merge, filter, sort and totals (reelsLibrary.test.js)
```
+Test files are not in this tree. A `(name.test.js)` or `with *.test.js` note above means the helper has a test in `frontend/tests/` at the mirrored path: `src/features/analytics/channel/channelChartData.js` is covered by `frontend/tests/features/analytics/channel/channelChartData.test.js`, and `src/docs/nav.js` by `frontend/tests/docs/nav.test.js`. See `docs/workflows/TESTING.md`.
+
## Metric Naming
Reddit exposes a post's net upvotes as `score` in its JSON API. In the UI this is always
diff --git a/docs/architecture/frontend/REACT_ARCHITECTURE.md b/docs/architecture/frontend/REACT_ARCHITECTURE.md
index ad176e3..28c105a 100644
--- a/docs/architecture/frontend/REACT_ARCHITECTURE.md
+++ b/docs/architecture/frontend/REACT_ARCHITECTURE.md
@@ -69,7 +69,6 @@ frontend/src/
library.js Filter tiers, paging, duration and engagement helpers
commandSearch.js Command palette ranking
holdProgress.js Hold-to-confirm timing
- *.test.js Unit tests (npm test, Node test runner)
services/
api.js All backend HTTP calls
context/
@@ -116,6 +115,12 @@ frontend/src/
Settings.jsx API keys status + GitHub config (page; sections, setup guide, hooks and helpers beside it)
```
+Unit tests are outside `src/`, in `frontend/tests/`, which mirrors this tree: `src/lib/accounts.js` is covered by `frontend/tests/lib/accounts.test.js`. They run on the Node test runner with `npm test` (see `docs/workflows/TESTING.md`), and Vite never bundles them.
+
+## Packages and Tooling
+
+`frontend/` is a workspace of the root npm workspaces monorepo. Install from the repository root with `npm install`, and add a frontend package with `npm install <pkg> --workspace frontend`; there is no `frontend/package-lock.json`. Shared dev tooling (ESLint and its plugins, `globals`, Prettier, concurrently) is in the root `package.json`, and the lint rules are in the root `eslint.config.mjs`. From the root, `npm run dev:frontend` starts Vite on port 5173, `npm run dev` starts it with the Worker, and `npm run build` builds `frontend/dist/`.
+
## Naming Conventions
- Feature files: `PascalCase.jsx` (matches component name)
diff --git a/docs/features/NEW_API_ENDPOINT.md b/docs/features/NEW_API_ENDPOINT.md
index 46af7ae..bd3d279 100644
--- a/docs/features/NEW_API_ENDPOINT.md
+++ b/docs/features/NEW_API_ENDPOINT.md
@@ -116,9 +116,11 @@ Add the new routes to `docs/architecture/backend/ROUTES.md`.
## Testing
+Pure logic behind the route (validation, response shaping) gets a unit test in `backend/tests/`, at the path that mirrors the source file: a handler in `backend/routes/myFeature.js` is tested in `backend/tests/routes/myFeature.test.js`. Route tests run the real handler against the fakes in `backend/tests/fixtures/` (`routeHarness.js`, `fakeSupabase.js`, `fakeFetch.js`) and compare the result with a `*.golden.json` snapshot there; see `backend/tests/routes/accounts.test.js` and `docs/workflows/TESTING.md`. Run `npm test` from the repository root.
+
```bash
-# Local test
-cd backend && wrangler dev
+# Local test, from the repository root
+npm run dev:backend
# Test with curl (replace TOKEN)
curl http://localhost:8787/api/my-resources \
diff --git a/docs/features/NEW_SCHEDULED_TASK.md b/docs/features/NEW_SCHEDULED_TASK.md
index 21ad41f..3c0bad8 100644
--- a/docs/features/NEW_SCHEDULED_TASK.md
+++ b/docs/features/NEW_SCHEDULED_TASK.md
@@ -75,7 +75,7 @@ Or match by the cron expression string - the `event.cron` property contains the
### Step 4: Test Locally
```bash
-cd backend && wrangler dev --test-scheduled
+cd backend && npx wrangler dev --test-scheduled
# Trigger manually:
curl "http://localhost:8787/__scheduled?cron=30+3+*+*+*"
```
@@ -83,7 +83,7 @@ curl "http://localhost:8787/__scheduled?cron=30+3+*+*+*"
### Step 5: Deploy
```bash
-cd backend && wrangler deploy
+cd backend && npx wrangler deploy
```
Cron triggers deploy with the Worker. Verify in the Cloudflare dashboard -> Workers -> Triggers -> Cron.
@@ -139,8 +139,11 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
- node-version: '20'
- - run: npm install @supabase/supabase-js
+ node-version: 24
+ cache: 'npm'
+ cache-dependency-path: package-lock.json
+ # one install at the root; @supabase/supabase-js is already in the root lockfile
+ - run: npm ci
- name: Run task
env:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
@@ -148,6 +151,8 @@ jobs:
run: node scripts/my-task.js
```
+Install from the root lockfile with `npm ci`, as the deploy workflows do. Do not run `npm install <pkg>` in the workflow: at the root it would add the package to the root `package.json`, and there is no lockfile inside `backend/` or `frontend/` to install from. If the script needs a package that is not in the lockfile yet, add it to the workspace that owns it (with approval) and commit the root lockfile. `scripts/publish-scheduled.js` is the exception by design: it has no dependencies, so its workflow skips the install.
+
### Step 3: Add Required Secrets
If the task requires secrets not already in GitHub Actions:
diff --git a/docs/guidelines/CLOUDFLARE_WORKERS.md b/docs/guidelines/CLOUDFLARE_WORKERS.md
index 089d245..ce2b644 100644
--- a/docs/guidelines/CLOUDFLARE_WORKERS.md
+++ b/docs/guidelines/CLOUDFLARE_WORKERS.md
@@ -103,20 +103,24 @@ This pattern is used in `backend/lib/oauthState.js` for OAuth state tokens.
## Local Development
+From the repository root, after a single `npm install` there:
+
```bash
-cd backend
-wrangler dev # reads .dev.vars for secrets
+npm run dev:backend # wrangler dev in backend/; reads backend/.dev.vars for secrets
+npm run dev # the Worker and the Vite dev server together
```
-The Worker runs at `http://localhost:8787` during local development.
+The Worker runs at `http://localhost:8787` during local development and reloads on save; no separate file watcher is needed.
## Deployment
```bash
cd backend
-wrangler deploy
+npx wrangler deploy
```
+`npx wrangler` runs the Wrangler version pinned in the root `package-lock.json`.
+
Or push to `main` with changes in `backend/` - GitHub Actions deploys automatically via `deploy-backend.yml`.
## CORS
diff --git a/docs/guidelines/JAVASCRIPT.md b/docs/guidelines/JAVASCRIPT.md
index 17576b4..7c5d4a0 100644
--- a/docs/guidelines/JAVASCRIPT.md
+++ b/docs/guidelines/JAVASCRIPT.md
@@ -128,3 +128,18 @@ const accounts = data ?? [];
## No Unused Code
Do not leave unused variables, unused imports, or commented-out code blocks. Remove them.
+
+## Lint and Format
+
+The tooling config lives once at the repository root and covers both apps:
+
+| File | What it sets |
+|---|---|
+| `eslint.config.mjs` | ESLint flat config. Frontend: recommended, React hooks and React Refresh rules with browser globals. Backend: recommended rules with service worker globals (the Worker has no Node APIs). `backend/tests/`, `scripts/` and root config files: recommended rules with Node globals. |
+| `.prettierrc.json` | Single quotes, semicolons, trailing commas, `arrowParens: "avoid"`, 120 columns, 2-space indentation, 4 spaces in `backend/` |
+| `.prettierignore` | Skips `node_modules`, build output, `package-lock.json`, the shadcn files in `frontend/src/components/ui`, the backend golden snapshots and Markdown |
+| `.editorconfig` | Editor defaults: UTF-8, 2-space indentation, 4 in `backend/`, final newline, trimmed trailing whitespace except in Markdown |
+
+- Run `npm run lint` from the root before every commit. Unused variables are errors; in the backend, prefix an intentionally unused variable or argument with `_`.
+- Match the indentation and quote style of the file you edit. The existing code was not reformatted in bulk; `npm run format` applies Prettier to the whole repository and belongs in its own commit when it is run.
+- Do not add per-app ESLint or Prettier config files. Change the root config instead, and update this section and `docs/workflows/TESTING.md`.
diff --git a/docs/guidelines/SHADCN.md b/docs/guidelines/SHADCN.md
index 045816e..96d898e 100644
--- a/docs/guidelines/SHADCN.md
+++ b/docs/guidelines/SHADCN.md
@@ -31,9 +31,11 @@ Never import from `shadcn/ui` directly - the components are copied into the proj
```bash
cd frontend
-npx shadcn@latest add <component-name>
+npx shadcn add <component-name>
```
+Run the CLI from `frontend/`, where `components.json` lives. The `shadcn` CLI is a dependency of the frontend workspace, so `npx shadcn` uses the version pinned in the root `package-lock.json`. If the CLI installs npm packages for a component, check afterwards that no `frontend/package-lock.json` was created; the root lockfile is the only one, and a stray workspace lockfile should be deleted and `npm install` run again at the root.
+
This copies the component source into `frontend/src/components/ui/`. After adding, verify the generated file uses `.jsx` (not `.tsx`). The `components.json` configuration should handle this automatically.
## Composition Rules
diff --git a/docs/workflows/DEPLOYMENT.md b/docs/workflows/DEPLOYMENT.md
index 24e43f7..d3436ee 100644
--- a/docs/workflows/DEPLOYMENT.md
+++ b/docs/workflows/DEPLOYMENT.md
@@ -9,48 +9,58 @@ Push to `main` -> GitHub Actions deploys automatically.
| `backend/**` | `deploy-backend.yml` | Cloudflare Worker |
| `frontend/**` | `deploy-frontend.yml` | Cloudflare Pages |
| Both | Both workflows | Both (in parallel) |
+| Root `package.json` or `package-lock.json` | Both workflows | Both (in parallel) |
| `docs/`, `scripts/`, `*.md` | Neither | Nothing deployed |
Monitor deployment status at:
`https://github.com/spacesdrive/twiligent/actions`
+Both deploy workflows set up Node 24, run `npm ci` once at the repository root (the root `package-lock.json` is the only lockfile), and deploy with the Wrangler version pinned in that lockfile. See `docs/architecture/cloudflare/CI_CD.md`.
+
## Manual Deployment (Local)
+The repository is an npm workspaces monorepo. Install once at the root; never run `npm install` inside `backend/` or `frontend/`:
+
+```bash
+npm ci
+```
+
+Wrangler is a backend dev dependency, so `npx wrangler` runs the lockfile version from any folder in the repo. Authenticate once with `npx wrangler login`.
+
### Backend (Cloudflare Worker)
```bash
cd backend
-wrangler deploy
+npx wrangler deploy
```
-Requires Cloudflare CLI (`wrangler`) and authenticated with `wrangler login`.
-
### Frontend (Cloudflare Pages)
```bash
-cd frontend
-npm ci
npm run build
-wrangler pages deploy dist --project-name=twiligent
+cd frontend
+npx wrangler pages deploy dist --project-name=twiligent
```
+`npm run build` at the root builds the frontend workspace into `frontend/dist/`.
+
## Setting Worker Secrets
-Secrets must be set before the first deploy or when adding new ones:
+Secrets must be set before the first deploy or when adding new ones. Run these from `backend/` so Wrangler picks up `wrangler.toml`:
```bash
-wrangler secret put SUPABASE_URL
-wrangler secret put SUPABASE_SERVICE_KEY
-wrangler secret put SUPABASE_ANON_KEY
-wrangler secret put YOUTUBE_API_KEY
-wrangler secret put INSTAGRAM_APP_ID
-wrangler secret put INSTAGRAM_APP_SECRET
-wrangler secret put CLOUDINARY_CLOUD_NAME
-wrangler secret put CLOUDINARY_UPLOAD_PRESET
-wrangler secret put BACKEND_URL
-wrangler secret put FRONTEND_URL
-wrangler secret put UPSTASH_REDIS_REST_URL # optional
-wrangler secret put UPSTASH_REDIS_REST_TOKEN # optional
+npx wrangler secret put SUPABASE_URL
+npx wrangler secret put SUPABASE_SERVICE_KEY
+npx wrangler secret put SUPABASE_ANON_KEY
+npx wrangler secret put YOUTUBE_API_KEY
+npx wrangler secret put INSTAGRAM_APP_ID
+npx wrangler secret put INSTAGRAM_APP_SECRET
+npx wrangler secret put CLOUDINARY_CLOUD_NAME
+npx wrangler secret put CLOUDINARY_UPLOAD_PRESET
+npx wrangler secret put BACKEND_URL
+npx wrangler secret put FRONTEND_URL
+npx wrangler secret put UPSTASH_REDIS_REST_URL # optional
+npx wrangler secret put UPSTASH_REDIS_REST_TOKEN # optional
```
Each command prompts for the value (no echo - the value is not shown).
@@ -99,11 +109,11 @@ GitHub Actions -> "Instagram Scheduled Publisher" -> "Run workflow" -> check run
**Backend:** Deploy a specific previous version:
```bash
-# List recent deployments
-wrangler deployments list
+# From backend/: list recent deployments
+npx wrangler deployments list
# Roll back to a specific deployment
-wrangler rollback <deployment-id>
+npx wrangler rollback <deployment-id>
```
**Frontend:** Cloudflare Pages -> deployments list -> "Rollback to this deployment" on a previous build.
@@ -117,11 +127,11 @@ For a fresh deployment from this repo:
3. Create Upstash Redis database (optional)
4. Create Cloudinary account + upload preset
5. Create Meta/Facebook Developer app with Instagram Graph API product
-6. `wrangler login`
+6. `npm ci` at the repository root, then `npx wrangler login`
7. Set all Worker secrets (see above)
8. Set all GitHub Actions secrets (see above)
-9. `wrangler deploy` (first manual deploy)
+9. `cd backend && npx wrangler deploy` (first manual deploy)
10. Verify health endpoint
-11. `cd frontend && npm run build && wrangler pages deploy dist --project-name=twiligent`
+11. `npm run build` at the root, then `cd frontend && npx wrangler pages deploy dist --project-name=twiligent`
12. Verify frontend loads
13. Add a YouTube channel via AccountManager to verify the full flow
diff --git a/docs/workflows/FEATURE_DEVELOPMENT.md b/docs/workflows/FEATURE_DEVELOPMENT.md
index d2570d9..b4da7a5 100644
--- a/docs/workflows/FEATURE_DEVELOPMENT.md
+++ b/docs/workflows/FEATURE_DEVELOPMENT.md
@@ -84,6 +84,9 @@ Before creating new patterns, check:
7. Frontend: new page/feature component
8. Frontend: `App.jsx` routing (if new page)
9. Frontend: `Sidebar.jsx` navigation (if new page needs nav entry)
+10. Tests for new pure logic, in the workspace's `tests/` folder at the path that mirrors the source file (`backend/services/youtube.js` -> `backend/tests/services/youtube.test.js`, `frontend/src/lib/accounts.js` -> `frontend/tests/lib/accounts.test.js`); see `docs/workflows/TESTING.md`
+
+If the feature needs a new npm package (approval required, see CLAUDE.md), add it to the app that uses it from the repository root: `npm install <pkg> --workspace backend` or `--workspace frontend`. Never run `npm install` inside `backend/` or `frontend/`; the root `package-lock.json` is the only lockfile.
**Step 12 - Eliminate duplication**
After implementing, look for logic that duplicates something that already existed. Extract it into the appropriate shared location (`lib/db.js`, `services/`, `utils/formatters.js`, shared components).
@@ -93,19 +96,18 @@ After implementing, look for logic that duplicates something that already existe
## Phase 5: Verify
**Step 13 - Build verification**
+Run from the repository root:
```bash
-# Backend
-cd backend && wrangler dev # should start without errors
-
-# Frontend
-cd frontend && npm run build # should build without errors
-cd frontend && npm run dev # should start without errors
+npm run build # frontend must build without errors
+npm test # backend and frontend unit tests must pass
+npm run dev # Worker (8787) and Vite (5173) must both start without errors
```
**Step 14 - Lint**
```bash
-cd frontend && npm run lint
+npm run lint # one ESLint run over backend, frontend, tests and scripts
```
+A change must not add lint errors. See `docs/workflows/TESTING.md` for the known pre-existing ones.
**Step 15 - Manual testing - golden path**
Test the main success case end-to-end:
diff --git a/docs/workflows/GIT.md b/docs/workflows/GIT.md
index daf05b7..714fdec 100644
--- a/docs/workflows/GIT.md
+++ b/docs/workflows/GIT.md
@@ -93,11 +93,13 @@ release: v1.1.0 - Reddit integration
- A new page + its route in App.jsx + its Sidebar entry
- A bug fix + updated docs if the fix changes documented behavior
- A new feature + CHANGELOG.md update + relevant architecture doc updates
+- A dependency change + the workspace `package.json` it touches + the root `package-lock.json`
### Never commit:
- `backend/.dev.vars`
- `backend/data/`
- `node_modules/`
+- `backend/package-lock.json` or `frontend/package-lock.json` (only the root lockfile is committed; a lockfile inside a workspace means `npm install` ran in the wrong folder)
- `.env` files with real credential values
- Cloudflare API tokens or Supabase service keys
- Editor or OS metadata files (`.DS_Store`, `Thumbs.db`)
@@ -180,12 +182,16 @@ git branch -d feat/reddit-integration
Run before every commit:
-1. `cd frontend && npm run build` - frontend must build without errors
-2. `cd frontend && npm run lint` - no lint errors
-3. `git status` - verify only intended files are staged
-4. `git diff --staged` - review the full diff before committing
-5. Check no secrets or credentials appear in the diff
-6. Verify CHANGELOG.md is updated if the commit adds a feature or fix
+Run the commands from the repository root:
+
+1. `npm run build` - frontend must build without errors
+2. `npm test` - backend and frontend unit tests must pass
+3. `npm run lint` - no new lint errors (one run covers backend, frontend, tests and scripts)
+4. `git status` - verify only intended files are staged
+5. `git diff --staged` - review the full diff before committing
+6. Check no secrets or credentials appear in the diff
+7. Check no lockfile appears inside `backend/` or `frontend/`; the root `package-lock.json` is the only lockfile
+8. Verify CHANGELOG.md is updated if the commit adds a feature or fix
---
@@ -195,6 +201,7 @@ After pushing to `main`, monitor GitHub Actions:
- `backend/` changes trigger `deploy-backend.yml`
- `frontend/` changes trigger `deploy-frontend.yml`
+- Root `package.json` or `package-lock.json` changes trigger both
- `scripts/` changes do not trigger automatic deployment
If a deployment workflow fails:
diff --git a/docs/workflows/TESTING.md b/docs/workflows/TESTING.md
index a1581fc..6df2e35 100644
--- a/docs/workflows/TESTING.md
+++ b/docs/workflows/TESTING.md
@@ -4,16 +4,34 @@
Most testing is manual. Both the backend and the frontend have unit tests for pure logic using Node's built-in test runner (no dependencies). Broader coverage is a known gap - see ROADMAP.md.
+Run every test from the repository root:
+
```bash
-cd backend && npm test
-cd frontend && npm test
+npm test # backend, then frontend
+npm test --workspace backend # backend only
+npm test --workspace frontend # frontend only
```
-Frontend tests (`npm test` runs every `src/**/*.test.js`) cover shared helpers in `src/lib/` and `src/utils/` (hold-to-confirm timing, command palette ranking, library filtering and paging, platform helpers, and a check that every Lucide icon imported in `src` exists) and page helpers that sit beside their feature in `src/features/**` and `src/docs/**`, such as `features/analytics/overview/overviewStats.js`. A testable helper is a plain `.js` file with no JSX and no `@/` imports, and its imports of project files are relative paths that end in `.js`, so Node can load it directly. This rule lives here, so helper files do not repeat it in a header comment.
+Each workspace's `test` script is `node --test "tests/**/*.test.js"`.
+
+### Test layout
+
+Tests never sit next to source files. Each workspace has a `tests/` folder that mirrors its source tree:
+
+| Source file | Test file |
+|---|---|
+| `backend/services/youtube.js` | `backend/tests/services/youtube.test.js` |
+| `backend/routes/accounts.js` | `backend/tests/routes/accounts.test.js` |
+| `backend/utils/scheduler.js` | `backend/tests/utils/scheduler.test.js` |
+| `frontend/src/lib/accounts.js` | `frontend/tests/lib/accounts.test.js` |
+| `frontend/src/features/analytics/overview/overviewStats.js` | `frontend/tests/features/analytics/overview/overviewStats.test.js` |
+| `frontend/src/docs/nav.js` | `frontend/tests/docs/nav.test.js` |
+
+Backend tests are in `backend/tests/lib`, `backend/tests/routes`, `backend/tests/services` and `backend/tests/utils`. Shared test helpers (`fakeSupabase.js`, `fakeFetch.js`, `golden.js`, `routeHarness.js`, the per-platform `*Fixtures.js` data) and the `*.golden.json` snapshots are in `backend/tests/fixtures/`. Wrangler only bundles files reachable from `server.js`, so nothing in `backend/tests/` ships in the Worker.
-Test files sit next to the module they cover as `*.test.js` (for example `services/instagram.test.js`). Wrangler only bundles files reachable from `server.js`, so test files never ship in the Worker.
+Frontend tests cover shared helpers in `src/lib/` and `src/utils/` (hold-to-confirm timing, command palette ranking, library filtering and paging, platform helpers, and a check that every Lucide icon imported in `src` exists) and page helpers in `src/features/**` and `src/docs/**`, such as `features/analytics/overview/overviewStats.js`. A testable helper is a plain `.js` file with no JSX and no `@/` imports, and its imports of project files are relative paths that end in `.js`, so Node can load it directly. This rule lives here, so helper files do not repeat it in a header comment.
-Backend route and scheduler tests (`routes/*.test.js`, `utils/scheduler.test.js`) run the real handlers against `services/fixtures/fakeSupabase.js` and a scripted `fetch`, then compare responses, queries and outgoing requests with the `services/fixtures/*.golden.json` snapshots. Rows tagged with `tableRow(name, row)` are only visible to that table; untagged rows are visible to every table. A refactor must leave the snapshots unchanged.
+Backend route and scheduler tests (`tests/routes/*.test.js`, `tests/utils/scheduler.test.js`) run the real handlers against `tests/fixtures/fakeSupabase.js` and a scripted `fetch`, then compare responses, queries and outgoing requests with the `tests/fixtures/*.golden.json` snapshots. Rows tagged with `tableRow(name, row)` are only visible to that table; untagged rows are visible to every table. A refactor must leave the snapshots unchanged. Prettier ignores the snapshots (`.prettierignore`), because the tests compare them byte for byte.
The testing workflow documented here describes what should run before shipping any change.
@@ -22,11 +40,14 @@ The testing workflow documented here describes what should run before shipping a
### 1. Build Verification
```bash
-# Frontend must build without errors
-cd frontend && npm run build
+# From the repository root: frontend must build without errors
+npm run build
-# Backend must be valid ESM (wrangler validates on deploy)
-cd backend && wrangler deploy --dry-run # if available
+# Unit tests must pass
+npm test
+
+# Backend must bundle (Wrangler validates without deploying)
+cd backend && npx wrangler deploy --dry-run
```
A broken build is a hard blocker. Do not deploy if build fails.
@@ -34,13 +55,19 @@ A broken build is a hard blocker. Do not deploy if build fails.
### 2. Lint
```bash
-cd frontend && npm run lint
+npm run lint
```
-Fix all lint errors. ESLint is configured via `frontend/eslint.config.js`. The rules enforce:
-- React hooks rules (no conditional hooks)
-- Unused variable detection
-- Import/export correctness
+One ESLint run from the root covers the whole repository. The shared config is `eslint.config.mjs` at the root:
+- `frontend/**`: the ESLint recommended rules, React hooks rules (no conditional hooks) and React Refresh rules, with browser globals; unused variables are errors unless they start with a capital letter or underscore
+- `backend/**`: the ESLint recommended rules with service worker globals, because the Worker runs on the web platform, not Node; unused variables and arguments starting with `_` are allowed, and unused caught errors are not reported
+- `backend/tests/**`, `scripts/**` and root config files: the ESLint recommended rules with Node globals
+
+Do not add lint errors. The frontend has 8 known errors that predate the shared config: `react-refresh/only-export-components` in `components/ui/badge.jsx`, `button.jsx`, `sidebar.jsx`, `tabs.jsx` and `toggle.jsx` and in `context/AppContext.jsx` and `AuthContext.jsx`, and `no-undef` for `__dirname` in `vite.config.js`. Any other error is new.
+
+### Formatting
+
+Prettier and EditorConfig are configured at the root (`.prettierrc.json`, `.prettierignore`, `.editorconfig`): single quotes, semicolons, trailing commas, no parentheses around a single arrow function parameter, 120 columns, 2-space indentation, and 4 spaces in `backend/`. The existing code has not been reformatted in bulk, and no check enforces formatting. Run `npm run format` (Prettier `--write` over the whole repository) only when you intend to reformat; it skips Markdown, the shadcn files in `frontend/src/components/ui`, the golden snapshots and build output.
### 3. Manual API Verification (Backend)
@@ -56,7 +83,7 @@ curl -H "Authorization: Bearer TOKEN" https://twiligent.ujjwalkrai.workers.dev/a
For local testing:
```bash
-cd backend && wrangler dev
+npm run dev:backend # from the repository root; wrangler dev on port 8787
# then curl http://localhost:8787/api/...
```
diff --git a/frontend/src/docs/pages/reference/Environment.jsx b/frontend/src/docs/pages/reference/Environment.jsx
index 726df18..d5858f6 100644
--- a/frontend/src/docs/pages/reference/Environment.jsx
+++ b/frontend/src/docs/pages/reference/Environment.jsx
@@ -55,7 +55,7 @@ export default function Environment() {
<P>Platform keys are shared by every member of the instance. Missing ones only switch off that platform: Settings shows which are set, and members see a clear message instead of an error.</P>
<H2>Frontend (Cloudflare Pages)</H2>
- <P>Set these in <Code>frontend/.env</Code> before <Code>npm run build</Code>, or as GitHub secrets when the deploy workflow builds for you. They are built into the site and are public.</P>
+ <P>Set these in <Code>frontend/.env</Code> before running <Code>npm run build</Code> in the top folder of the project, or as GitHub secrets when the deploy workflow builds for you. They are built into the site and are public.</P>
<Table
head={['Name', 'Needed', 'Value']}
rows={FRONTEND_VARS.map(([name, need, value]) => [<Code>{name}</Code>, NEED[need], value])}
diff --git a/frontend/src/docs/pages/selfHost/DeployBackend.jsx b/frontend/src/docs/pages/selfHost/DeployBackend.jsx
index c244a64..f3f2fb8 100644
--- a/frontend/src/docs/pages/selfHost/DeployBackend.jsx
+++ b/frontend/src/docs/pages/selfHost/DeployBackend.jsx
@@ -7,12 +7,13 @@ export default function DeployBackend() {
<DocTitle title="Deploy the backend" lead="Put the Worker on Cloudflare and give it every secret." />
<Steps>
<Step title="Install and sign in">
+ <P>Run these in the top folder of your copy of the project. One <Code>npm install</Code> there installs the backend and the website together; do not run it inside <Code>backend</Code> or <Code>frontend</Code>.</P>
<CodeBlock>{`
-cd backend
npm install
+cd backend
npx wrangler login
`}</CodeBlock>
- <P>A browser window opens to sign in to Cloudflare. Create a free account if you do not have one.</P>
+ <P>A browser window opens to sign in to Cloudflare. Create a free account if you do not have one. Run the rest of the commands on this page from the <Code>backend</Code> folder.</P>
</Step>
<Step title="Deploy once to get your URL">
<CodeBlock>{'npx wrangler deploy'}</CodeBlock>
@@ -50,7 +51,7 @@ npx wrangler secret put FRONTEND_URL
/>
<H2>Run it locally</H2>
- <P>Create <Code>backend/.dev.vars</Code> with the same names and values, one per line as <Code>NAME=value</Code>, with <Code>{'BACKEND_URL=http://localhost:8787'}</Code> and <Code>{'FRONTEND_URL=http://localhost:5173'}</Code>. Then run <Code>npx wrangler dev</Code>. The file is ignored by Git; never commit it.</P>
+ <P>Create <Code>backend/.dev.vars</Code> with the same names and values, one per line as <Code>NAME=value</Code>, with <Code>{'BACKEND_URL=http://localhost:8787'}</Code> and <Code>{'FRONTEND_URL=http://localhost:5173'}</Code>. Then run <Code>npm run dev</Code> in the top folder of the project to start the backend on port 8787 and the website on port 5173 together, or <Code>npm run dev:backend</Code> for the backend alone. The file is ignored by Git; never commit it.</P>
</>
);
}
diff --git a/frontend/src/docs/pages/selfHost/DeployFrontend.jsx b/frontend/src/docs/pages/selfHost/DeployFrontend.jsx
index a1ec653..f2afb41 100644
--- a/frontend/src/docs/pages/selfHost/DeployFrontend.jsx
+++ b/frontend/src/docs/pages/selfHost/DeployFrontend.jsx
@@ -19,10 +19,10 @@ [email protected]
<P><Code>VITE_API_URL</Code> is your backend URL followed by <Code>/api</Code>. Leaving off <Code>/api</Code> is the most common setup mistake.</P>
</Step>
<Step title="Build and deploy">
+ <P>Run these in the top folder of the project. If you have not installed yet, run <Code>npm install</Code> there first; it covers the website too.</P>
<CodeBlock>{`
-cd frontend
-npm install
npm run build
+cd frontend
npx wrangler pages deploy dist --project-name=twiligent
`}</CodeBlock>
<P>The first time, Wrangler creates the Pages project and prints the address, for example <Code>https://twiligent.pages.dev</Code>.</P>
diff --git a/frontend/src/docs/pages/selfHost/GitHub.jsx b/frontend/src/docs/pages/selfHost/GitHub.jsx
index 2362cc9..2ebb62d 100644
--- a/frontend/src/docs/pages/selfHost/GitHub.jsx
+++ b/frontend/src/docs/pages/selfHost/GitHub.jsx
@@ -8,7 +8,7 @@ export default function GitHub() {
<P>Both parts are optional. Fork <A href="https://github.com/spacesdrive/twiligent">spacesdrive/twiligent</A> to your own GitHub account first. Add secrets in your fork under <strong>Settings</strong>, <strong>Secrets and variables</strong>, <strong>Actions</strong>, <strong>New repository secret</strong>.</P>
<H2>Deploy on push</H2>
- <P>The workflows <Code>deploy-backend.yml</Code> and <Code>deploy-frontend.yml</Code> deploy whenever <Code>backend/</Code> or <Code>frontend/</Code> changes on <Code>main</Code>.</P>
+ <P>The workflows <Code>deploy-backend.yml</Code> and <Code>deploy-frontend.yml</Code> deploy whenever <Code>backend/</Code> or <Code>frontend/</Code> changes on <Code>main</Code>. A change to <Code>package.json</Code> or <Code>package-lock.json</Code> in the top folder deploys both, because those files list the packages for the whole project. Each workflow installs the packages itself, so there is nothing to install on GitHub.</P>
<Table
head={['Secret', 'Where to get it']}
rows={[
diff --git a/frontend/src/docs/pages/selfHost/Overview.jsx b/frontend/src/docs/pages/selfHost/Overview.jsx
index 3fd737c..48c555a 100644
--- a/frontend/src/docs/pages/selfHost/Overview.jsx
+++ b/frontend/src/docs/pages/selfHost/Overview.jsx
@@ -40,8 +40,9 @@ export default function Overview() {
<H2>What you need on your computer</H2>
<UL>
- <li>Node.js 20 or newer, and Git.</li>
+ <li>Node.js 22 or newer, and Git.</li>
<li>A copy of the code: <Code>git clone https://github.com/spacesdrive/twiligent.git</Code></li>
+ <li>The project's packages: run <Code>npm install</Code> once in the cloned <Code>twiligent</Code> folder. It installs the backend, the website and the Wrangler command used to deploy them.</li>
</UL>
</>
);