diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..bda8bc2 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,86 @@ +name: Lint + +on: + push: + branches: + - "**" + paths: + - '.changeset/**' + - '.github/workflows/**' + - 'backend/**' + - 'packages/**' + - 'scripts/**' + - 'package.json' + - 'package-lock.json' + pull_request: + branches: + - main + - master + paths: + - '.changeset/**' + - '.github/workflows/**' + - 'backend/**' + - 'packages/**' + - 'scripts/**' + - 'package.json' + - 'package-lock.json' + workflow_dispatch: {} + +jobs: + node: + name: Lint (Node/Svelte) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '24' + cache: npm + cache-dependency-path: package-lock.json + - name: Install dependencies + run: npm ci + - name: Run Node/Svelte lint checks + run: npm run lint + + go: + name: Lint (Go) + runs-on: ubuntu-latest + permissions: + contents: read + defaults: + run: + working-directory: backend + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: backend/go.mod + cache: true + - name: Check gofmt + shell: bash + run: test -z "$(gofmt -l $(find . -type f -name '*.go'))" + - name: Run go vet + run: go vet ./... + + python: + name: Lint (Python) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install package and Ruff + run: python -m pip install --upgrade pip ruff -e packages/python-backend + - name: Run Ruff + run: ruff check packages/python-backend/src packages/python-backend/tests diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..b5fe94f --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,83 @@ +name: Tests + +on: + push: + branches: + - "**" + paths: + - '.changeset/**' + - '.github/workflows/**' + - 'backend/**' + - 'packages/**' + - 'scripts/**' + - 'package.json' + - 'package-lock.json' + pull_request: + branches: + - main + - master + paths: + - '.changeset/**' + - '.github/workflows/**' + - 'backend/**' + - 'packages/**' + - 'scripts/**' + - 'package.json' + - 'package-lock.json' + workflow_dispatch: {} + +jobs: + node: + name: Test (Node/Svelte) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '24' + cache: npm + cache-dependency-path: package-lock.json + - name: Install dependencies + run: npm ci + - name: Build and test Node/Svelte packages + run: npm test + + go: + name: Test (Go) + runs-on: ubuntu-latest + permissions: + contents: read + defaults: + run: + working-directory: backend + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: backend/go.mod + cache: true + - name: Run Go tests + run: go test ./... + + python: + name: Test (Python) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install package + run: python -m pip install --upgrade pip -e packages/python-backend + - name: Run Python tests + run: python -m unittest discover -s packages/python-backend/tests -p 'test_*.py' diff --git a/package.json b/package.json index 7a56557..04850dc 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "check": "npm run check --workspace=@gabriel20xx/web-ui", "build": "npm run package --workspace=@gabriel20xx/web-ui", "package": "npm run package --workspace=@gabriel20xx/web-ui", + "lint": "npm run check && npm run lint:node", + "lint:node": "node scripts/check-node-syntax.mjs", + "test": "npm run build && npm run test:node-backend", "test:node-backend": "npm test --workspace=@gabriel20xx/node-backend", "changeset": "changeset", "version-packages": "changeset version", diff --git a/packages/python-backend/pyproject.toml b/packages/python-backend/pyproject.toml index 1679a2e..9735dc8 100644 --- a/packages/python-backend/pyproject.toml +++ b/packages/python-backend/pyproject.toml @@ -16,3 +16,10 @@ dependencies = [ [tool.setuptools.packages.find] where = ["src"] + +[tool.ruff] +target-version = "py310" +line-length = 100 + +[tool.ruff.lint] +select = ["E", "F", "I"] diff --git a/packages/python-backend/src/shared_backend/auth.py b/packages/python-backend/src/shared_backend/auth.py index 88cadec..e6312c6 100644 --- a/packages/python-backend/src/shared_backend/auth.py +++ b/packages/python-backend/src/shared_backend/auth.py @@ -22,7 +22,8 @@ CREATE TABLE IF NOT EXISTS auth_users ( ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS email TEXT NOT NULL DEFAULT ''; ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT now(); ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now(); -CREATE UNIQUE INDEX IF NOT EXISTS auth_users_email_idx ON auth_users (lower(email)) WHERE email <> ''; +CREATE UNIQUE INDEX IF NOT EXISTS auth_users_email_idx + ON auth_users (lower(email)) WHERE email <> ''; CREATE TABLE IF NOT EXISTS auth_sessions ( id BIGSERIAL PRIMARY KEY, user_id BIGINT NOT NULL REFERENCES auth_users(id) ON DELETE CASCADE, diff --git a/packages/python-backend/src/shared_backend/postgres.py b/packages/python-backend/src/shared_backend/postgres.py index 60bd0bc..ca39551 100644 --- a/packages/python-backend/src/shared_backend/postgres.py +++ b/packages/python-backend/src/shared_backend/postgres.py @@ -3,7 +3,7 @@ from __future__ import annotations from contextlib import asynccontextmanager -from typing import Any, AsyncIterator, Awaitable, Callable, Sequence +from typing import Any, AsyncIterator, Awaitable, Callable import asyncpg diff --git a/packages/python-backend/tests/test_core.py b/packages/python-backend/tests/test_core.py new file mode 100644 index 0000000..63d386e --- /dev/null +++ b/packages/python-backend/tests/test_core.py @@ -0,0 +1,34 @@ +import os +import unittest + +from shared_backend.auth import hash_password, verify_password +from shared_backend.config import DatabaseConfig +from shared_backend.tokens import hash_token, new_session_token + + +class SharedBackendTests(unittest.TestCase): + def test_password_round_trip(self): + password_hash = hash_password("correct horse battery staple") + self.assertTrue(verify_password("correct horse battery staple", password_hash)) + self.assertFalse(verify_password("wrong password", password_hash)) + + def test_session_token_hash(self): + token, token_hash = new_session_token() + self.assertNotEqual(token, token_hash) + self.assertEqual(hash_token(token), token_hash) + + def test_database_config_from_environment(self): + config = DatabaseConfig.from_env( + { + **os.environ, + "DATABASE_URL": "postgresql://user:password@localhost:5432/app", + "DATABASE_MAX_CONNS": "7", + "DATABASE_MIN_CONNS": "2", + } + ) + self.assertEqual(config.max_size, 7) + self.assertEqual(config.min_size, 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/check-node-syntax.mjs b/scripts/check-node-syntax.mjs new file mode 100644 index 0000000..eb39065 --- /dev/null +++ b/scripts/check-node-syntax.mjs @@ -0,0 +1,26 @@ +import { execFileSync } from 'node:child_process'; +import { readdirSync, statSync } from 'node:fs'; +import { join, relative } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const root = fileURLToPath(new URL('..', import.meta.url)); +const directories = ['packages/node-backend/src', 'packages/node-backend/test']; + +function javascriptFiles(directory) { + const absoluteDirectory = join(root, directory); + const files = []; + for (const entry of readdirSync(absoluteDirectory)) { + const absolutePath = join(absoluteDirectory, entry); + if (statSync(absolutePath).isFile() && /\.(?:c|m)?js$/.test(entry)) { + files.push(absolutePath); + } + } + return files; +} + +for (const directory of directories) { + for (const file of javascriptFiles(directory)) { + execFileSync(process.execPath, ['--check', file], { stdio: 'inherit' }); + console.log(`Syntax OK: ${relative(root, file)}`); + } +}