diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75e4fcb0..e5af1e25 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -153,6 +153,9 @@ jobs: - name: Check formatting run: cargo fmt --all -- --check + - name: Check every source file is a declared module + run: bash scripts/check_modules_reachable.sh + - name: Clippy (lint) run: cargo clippy --all-targets --all-features -- -D warnings diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 07dfe192..43456c8d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,3 +16,13 @@ Follow existing conventions. Write tests for new features. ## Reporting Issues Include: steps to reproduce, expected behavior, environment info. + +## Rust formatting + +CI runs `cargo fmt --all -- --check` for `contract/`. Enable the repo hook once so it is checked before every commit: + +```bash +git config core.hooksPath .githooks +``` + +and run `cd contract && cargo fmt --all` if it complains. diff --git a/contract/scripts/check_modules_reachable.sh b/contract/scripts/check_modules_reachable.sh new file mode 100755 index 00000000..a901c878 --- /dev/null +++ b/contract/scripts/check_modules_reachable.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# Fail if a .rs file under src/ is not reachable from lib.rs / main.rs. +# A file nobody declares as a module is never compiled, so its tests never run. +set -euo pipefail +cd "$(dirname "$0")/.." + +fail=0 +while IFS= read -r f; do + rel="${f#src/}" + case "$rel" in lib.rs|main.rs) continue ;; esac + dir="$(dirname "$rel")" + base="$(basename "$rel" .rs)" + if [ "$base" = "mod" ]; then + # src/a/b/mod.rs is declared as `mod b;` in src/a/mod.rs (or lib.rs for src/b/mod.rs) + name="$(basename "$dir")" + parent="$(dirname "$dir")" + else + name="$base" + parent="$dir" + fi + if [ "$parent" = "." ]; then + candidates="src/lib.rs src/main.rs" + else + candidates="src/$parent/mod.rs src/$parent.rs" + fi + found=0 + for c in $candidates; do + if [ -f "$c" ] && grep -Eq "^[[:space:]]*(pub(\([a-z]+\))?[[:space:]]+)?mod[[:space:]]+${name}[[:space:]]*;" "$c"; then + found=1 + fi + done + if [ "$found" -eq 0 ]; then + echo "ERROR: $f is not declared as a module (it is never compiled)" + fail=1 + fi +done < <(find src -name '*.rs' | sort) + +exit "$fail" diff --git a/contract/src/lib.rs b/contract/src/lib.rs index c0cf4b72..0df59423 100644 --- a/contract/src/lib.rs +++ b/contract/src/lib.rs @@ -14,7 +14,7 @@ //! //! | Module | Why it is public | //! |---|---| -//! | `app` | The Axum `Router` constructor. This is the entry point for embedding the service. | +//! | *(root)* `app` | Not a module: `pub fn app(state)` at the crate root builds the Axum `Router` (it delegates to `routes::app`). This is the entry point for embedding the service. | //! | `cache` | `CacheBackend` / `InMemoryCache` / `RedisCache` are named in `AppState`, which integration tests construct. | //! | `config` | `AppConfig::from_env` and `ConfigError` are how a deployment is configured. | //! | `hash_validator` | `HashValidator` and `ValidationError` are the contract for callers accepting document hashes. | @@ -42,7 +42,6 @@ //! Until then the rule is: **do not add new `pub` items to these modules**, and //! prefer `pub(crate)` for anything new that does not have to be reachable. -pub mod app; pub mod cache; pub mod config; pub mod error; @@ -59,6 +58,9 @@ pub mod types; pub mod webhook; pub mod smalda_helpers; +#[cfg(test)] +mod tests; + use axum::{ body::Body, extract::{Path, State}, @@ -1419,7 +1421,7 @@ pub fn find_duplicates(documents: &[&str], threshold: f64) -> Vec<(usize, usize, } #[cfg(test)] -mod tests { +mod unit_tests { use super::*; #[test] diff --git a/contract/src/tests/mod.rs b/contract/src/tests/mod.rs index 5155b774..f3cd0d06 100644 --- a/contract/src/tests/mod.rs +++ b/contract/src/tests/mod.rs @@ -1 +1,4 @@ -pub mod integration; +mod cache_key_namespace; +mod error_response_no_leak; +mod integration; +mod stellar_retry_classification; diff --git a/contract/tests/public_api_surface.rs b/contract/tests/public_api_surface.rs index f714d618..bbe0cb6a 100644 --- a/contract/tests/public_api_surface.rs +++ b/contract/tests/public_api_surface.rs @@ -47,7 +47,6 @@ fn declared_modules(source: &str) -> Vec<&str> { /// Grouped the way the doc comment groups them, so the two lists cannot drift /// apart silently. const DELIBERATELY_PUBLIC: &[&str] = &[ - "app", "cache", "config", "hash_validator", diff --git a/frontend/app/[locale]/(protected)/documents/[id]/page.tsx b/frontend/app/[locale]/(protected)/documents/[id]/page.tsx index b023f76c..1711c8b9 100644 --- a/frontend/app/[locale]/(protected)/documents/[id]/page.tsx +++ b/frontend/app/[locale]/(protected)/documents/[id]/page.tsx @@ -2,10 +2,11 @@ import React, { useCallback, useEffect, useRef, useState } from "react"; import { useParams } from "next/navigation"; -import { useTranslations } from "next-intl"; +import { useLocale, useTranslations } from "next-intl"; import { Link, useRouter } from "@/i18n/navigation"; import { useDateFormatting } from "@/i18n/formatting"; import { ApiError, request } from "@/lib/api-client"; +import { riskUrl } from "@/lib/risk-url"; import { apiUrl } from "@/lib/api-config"; import { Dialog, @@ -262,6 +263,7 @@ function ShareModal({ docId }: { docId: string }) { export default function DocumentDetailPage() { const t = useTranslations("documentDetail"); + const locale = useLocale(); const { formatDateTime } = useDateFormatting(); const params = useParams<{ id: string }>(); const router = useRouter(); @@ -315,7 +317,7 @@ export default function DocumentDetailPage() { const [docData, riskData] = await Promise.all([ request(apiUrl(path)), // Risk data is optional; a missing endpoint must not fail the page. - request(apiUrl(`${path}/risk`)).catch(() => null), + request(riskUrl(path, locale)).catch(() => null), ]); setDoc(docData); setLatitudeInput(docData.latitude == null ? "" : String(docData.latitude)); @@ -330,7 +332,7 @@ export default function DocumentDetailPage() { } finally { setLoading(false); } - }, [docId, t]); + }, [docId, t, locale]); useEffect(() => { fetchData(); diff --git a/frontend/lib/__tests__/risk-url.test.ts b/frontend/lib/__tests__/risk-url.test.ts new file mode 100644 index 00000000..0ad8b163 --- /dev/null +++ b/frontend/lib/__tests__/risk-url.test.ts @@ -0,0 +1,9 @@ +import { riskUrl } from "../risk-url"; + +describe("riskUrl", () => { + it.each(["en", "fr", "es"])("sends the %s locale as ?lang=", (locale) => { + const url = riskUrl("/documents/abc", locale); + expect(url).toContain("/documents/abc/risk"); + expect(new URL(url).searchParams.get("lang")).toBe(locale); + }); +}); diff --git a/frontend/lib/risk-url.ts b/frontend/lib/risk-url.ts new file mode 100644 index 00000000..7cbe25cc --- /dev/null +++ b/frontend/lib/risk-url.ts @@ -0,0 +1,10 @@ +import { apiUrl } from "@/lib/api-config"; + +/** + * URL of a document's risk assessment, in the user's locale. The backend + * (`GET /documents/:id/risk?lang=`) returns the localised flag descriptions + * for `en`, `fr` and `es`; without `lang` they are always English. + */ +export function riskUrl(documentPath: string, locale: string): string { + return apiUrl(`${documentPath}/risk`, { lang: locale }); +}