File size: 7,736 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Development

This guide is for contributors working on SupoClip locally.

## Repository Layout

Current repository structure:

- `backend/`
  - FastAPI app
  - ARQ worker
  - services, repositories, route modules, and media-processing code
- `frontend/`
  - Next.js app
  - App Router pages, API routes, auth, Prisma schema, UI components
- Root files
  - `docker-compose.yml`
  - `init.sql`
  - `.env.example`
  - `start.sh`

## Main Commands

## Full stack with Docker

```bash
docker-compose up -d --build
docker-compose logs -f
docker-compose down
```

## Frontend

```bash
cd frontend
pnpm install --frozen-lockfile
pnpm run dev
pnpm run build
pnpm run start
pnpm run lint
```

## Backend

Install ffmpeg and Tesseract with English language data (`tesseract-ocr`, `tesseract-ocr-eng` on Debian/Ubuntu). The Docker image includes both. Tesseract checks on-screen interruption notices locally; it does not call an AI service.

```bash
cd backend
uv venv .venv
source .venv/bin/activate
uv sync
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
```

Run the worker separately:

```bash
cd backend
source .venv/bin/activate
arq src.workers.tasks.WorkerSettings
```

## Frontend Development Notes

Important locations:

- `frontend/src/app`
  - App pages and API routes
- `frontend/src/components`
  - Reusable UI and product components
- `frontend/src/lib`
  - Auth, API helpers, backend proxy helpers, Stripe wiring
- `frontend/prisma`
  - Prisma schema and migrations, if present in your branch
- `frontend/src/generated/prisma`
  - Generated Prisma client output

### Build behavior

The frontend build runs:

```bash
prisma generate && next build
```

`postinstall` also runs Prisma generation.

### Frontend patterns

- App Router
- Mostly client-side product pages
- Better Auth sessions
- No dedicated global state library

## Backend Development Notes

Important locations:

- `backend/src/main.py`
  - Active entry point
- `backend/src/api/routes`
  - Route modules
- `backend/src/services`
  - Business logic
- `backend/src/repositories`
  - Data access
- `backend/src/workers`
  - Queue processing
- `backend/src/video_utils.py`
  - Clip rendering pipeline
- `backend/src/ai.py`
  - LLM prompt and validation logic

### Layering guideline

When possible:

- keep HTTP concerns in route modules
- keep orchestration in services
- keep SQL and persistence in repositories

## Database Notes

The primary database bootstrap file is:

- `init.sql`

It defines:

- users
- sessions
- auth support tables
- tasks
- sources
- generated clips
- processing cache
- Stripe webhook tracking

The frontend also uses Prisma for auth and admin-related access patterns.

## Common Development Workflows

### Modify clip selection behavior

Primary files:

- `backend/src/ai.py`
- `backend/src/services/video_service.py`

Use this area when changing:

- segment selection rules
- LLM prompts
- output validation
- clip count heuristics

### Modify rendering or subtitle behavior

Primary files:

- `backend/src/video_utils.py`
- `backend/src/caption_templates.py`
- `backend/src/clip_editor.py`

Use this area when changing:

- subtitle layout
- font rendering
- cropping
- export presets
- clip edits after generation

### Modify task orchestration

Primary files:

- `backend/src/api/routes/tasks.py`
- `backend/src/services/task_service.py`
- `backend/src/workers/tasks.py`
- `backend/src/workers/job_queue.py`

Use this area when changing:

- status transitions
- background job behavior
- cancellation and resume logic
- progress reporting

### Modify uploads, fonts, transitions, or media listings

Primary files:

- `backend/src/api/routes/media.py`
- `backend/src/font_registry.py`
- `backend/fonts/`
- `backend/transitions/`

### Modify auth or user roles

Primary files:

- `frontend/src/lib/auth.ts`
- `frontend/src/app/api/auth/[...all]/route.ts`
- `init.sql`

### Modify billing behavior

Primary files:

- `frontend/src/app/api/billing/*`
- `frontend/src/lib/stripe.ts`
- `backend/src/api/routes/billing.py`
- `backend/src/services/billing_service.py`
- `backend/src/services/subscription_email_service.py`

### Modify the admin dashboard

Primary files:

- `frontend/src/app/admin/page.tsx`
- `backend/src/api/routes/admin.py`

## Testing and Verification

The repository now uses a three-layer automated test setup:

- backend `pytest` for unit and integration coverage
- frontend `Vitest` plus Testing Library for route handlers and client UI
- frontend `Playwright` for seeded browser smoke tests against real frontend and backend processes

Primary repo-level commands:

```bash
make test
make test-backend
make test-frontend
make test-e2e
make test-ci
```

Direct app-level commands:

```bash
cd backend && uv sync --all-groups && .venv/bin/pytest
cd frontend && pnpm install --frozen-lockfile && pnpm run test:coverage
cd frontend && pnpm run test:e2e
```

### Local Test Environment

- Start PostgreSQL and Redis locally before running integration or e2e flows.
- `docker-compose up -d postgres redis` is enough for backend and frontend test runs.
- `docker-compose up -d` is the simplest full-stack option when you also want manual smoke testing.

Useful backend test env vars:

```bash
DATABASE_URL=postgresql+asyncpg://localhost:5432/supoclip
TEST_DATABASE_URL=postgresql+asyncpg://localhost:5432/supoclip
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
BACKEND_AUTH_SECRET=supoclip_test_secret
BETTER_AUTH_SECRET=supoclip_better_auth_test_secret
```

### Coverage and CI

- Backend coverage thresholds are enforced during `pytest`.
- Frontend coverage thresholds are enforced during `pnpm run test:coverage`.
- GitHub Actions runs separate `backend`, `frontend`, and `e2e` jobs with Postgres and Redis service containers.
- Playwright failures retain traces, screenshots, and videos for debugging.

### Recommended Manual Smoke Test

Automated tests cover the main seams, but manual smoke testing is still useful for high-risk media flows:

1. Start the stack.
2. Sign in.
3. Create a task from a YouTube URL.
4. Confirm progress updates arrive.
5. Confirm clips are generated.
6. Confirm clip editing and export actions still work.

## Helpful Logs

```bash
docker-compose logs -f backend
docker-compose logs -f worker
docker-compose logs -f frontend
docker-compose logs -f postgres
docker-compose logs -f redis
```

## Codebase Conventions

### Backend

- Python 3.11+
- 4-space indentation
- Prefer type hints where practical
- `snake_case` naming

### Frontend

- TypeScript and React
- 2-space indentation
- `PascalCase` components
- `camelCase` variables and functions
- Use `@/*` imports where practical

## Safe Defaults for New Work

- Use `backend/src/main.py`; `main_refactored.py` is a compatibility import
- Keep auth-sensitive browser requests behind frontend API routes
- Preserve async behavior by keeping blocking work out of FastAPI request handlers
- Use the worker for long-running media processing

## Related Reading

- [Architecture](./architecture.md)
- [API Reference](./api-reference.md)
- [Troubleshooting](./troubleshooting.md)

### Isolated UI regression checks

Run `cd frontend && pnpm exec playwright install chromium && pnpm run test:polish`
for browser checks with mocked API responses. These cover failed caption saves,
editor navigation, narrow layouts, and generation-list updates without a database.
Run `make check` before submitting changes. Full database-backed smoke tests still
use `make test-e2e`.

### Real local processing checks

See [Testing with local YouTube downloads](./testing-local.md) for the opt-in
`pnpm run test:local` suite and real API/worker checks. These exercise yt-dlp,
Whisper, Ollama, PostgreSQL, Redis and FFmpeg without Apify or mocked processing.