Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ design is in `docs/design.md`; read it before changing the architecture, and upd
customer's document or data.
- Build Rust in CI or on a build machine (docspring-dev), not on small agent VMs. The server is app/ (Laravel Rust,
`visualci-app` in the root workspace, with the CLI and its crates in crates/, whose tests need no database).
`scripts/on-dev test` runs the crates' tests; app/'s (`cargo test -p visualci-app`) need docspring-dev's Postgres:
a `scripts/on-dev run` naming `DATABASE_URL` (such as `VISUALCI_APP_TEST_DATABASE_URL=...`) leases
docspring-dev itself, never the pool's rented box, which has no Postgres. Point them at a
database of your own, `visualci_app_test_<story>`.
`scripts/on-dev test` runs the crates' tests. app/'s (`cargo test -p visualci-app`) need Postgres and a blob store:
run them with `scripts/on-dev run` naming `VISUALCI_APP_TEST_DATABASE_URL=postgres://...@127.0.0.1:55433/visualci_app_test_<story>`.
On docspring-dev that's its own Postgres (a database of your own per story). On a pool box the run starts its own
Postgres and RustFS in Docker, points the command at them, and removes them when it ends.
- VisualCI dev ports on docspring-dev: the app 5250 (`SERVER_PORT=5250`, which the Vite proxy defaults to), Vite 5173,
previews 5180–5199. 5150 is
Talira's dev server: never use it (on 1 Dec 2026 a VisualCI dev server took it, and Talira's localhost and MCP
Expand Down
3 changes: 3 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ GITHUB_CLIENT_SECRET=
# postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test.
# VISUALCI_APP_TEST_DATABASE_URL=

# The tests' blob store (RustFS or MinIO). Default: CI's, http://127.0.0.1:59000.
# VISUALCI_APP_TEST_S3_ENDPOINT=

# A folder the email tests write each email's HTML to, for review.
# VISUALCI_EMAIL_SNAPSHOTS=

Expand Down
4 changes: 4 additions & 0 deletions app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ const DEVELOPMENT: &[Variable] = &[
"The tests' Postgres (each test borrows a copy of it). Default: CI's, \
`postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test`.",
),
Variable::new(
"VISUALCI_APP_TEST_S3_ENDPOINT",
"The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`.",
),
Variable::new("VISUALCI_EMAIL_SNAPSHOTS", "A folder the email tests write each email's HTML to, for review."),
Variable::new(
"VISUALCI_GOLDEN_UPDATE",
Expand Down
14 changes: 10 additions & 4 deletions app/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use laravel::Application;
use laravel::foundation::ApplicationBuilder;

const CI_DATABASE: &str = "postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test";
const CI_S3_ENDPOINT: &str = "http://127.0.0.1:59000";

pub fn create() -> Application {
configure().create()
Expand All @@ -19,6 +20,12 @@ pub fn database_url() -> String {
std::env::var("VISUALCI_APP_TEST_DATABASE_URL").unwrap_or_else(|_| CI_DATABASE.to_string())
}

/// The test blob store: `VISUALCI_APP_TEST_S3_ENDPOINT`, else CI's (RustFS on port 59000).
#[must_use]
pub fn s3_endpoint() -> String {
std::env::var("VISUALCI_APP_TEST_S3_ENDPOINT").unwrap_or_else(|_| CI_S3_ENDPOINT.to_string())
}

pub fn configure() -> ApplicationBuilder {
let url = database_url();
crate::configure()
Expand All @@ -38,8 +45,6 @@ pub fn configure() -> ApplicationBuilder {
("SESSION_DRIVER", "database"),
("WORKFLOW_STORE", "memory"),
("DB_CONNECTION", "pgsql"),
// The test blob store: RustFS in CI, MinIO on a development machine, both on port 59000.
("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"),
("VISUALCI_S3_BUCKET", "visualci-test"),
("VISUALCI_S3_ACCESS_KEY_ID", "visualci"),
("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"),
Expand All @@ -49,6 +54,7 @@ pub fn configure() -> ApplicationBuilder {
("VISUALCI_METRICS_TOKEN", "metrics-test-token"),
])
.with_env("DATABASE_URL", url)
.with_env("VISUALCI_S3_ENDPOINT", s3_endpoint())
.refresh_database()
}

Expand All @@ -73,12 +79,12 @@ pub mod fixtures {
/// The blob store, with its bucket made. Fails, naming the store, when it isn't running: a test never skips.
///
/// # Errors
/// When the store (`VISUALCI_S3_ENDPOINT`, RustFS or MinIO on port 59000 in development and CI) isn't reachable.
/// When the store (`VISUALCI_APP_TEST_S3_ENDPOINT`, else RustFS on port 59000 as in CI) isn't reachable.
pub async fn blob_store() -> laravel::Result<crate::storage::BlobStore> {
let store = crate::storage::BlobStore::current().await?;
store.ensure_bucket().await.map_err(|e| {
laravel::Error::msg(format!(
"the blob store isn't reachable (VISUALCI_S3_ENDPOINT, RustFS or MinIO on 127.0.0.1:59000 in CI): {e}"
"the blob store isn't reachable (VISUALCI_APP_TEST_S3_ENDPOINT, else RustFS on 127.0.0.1:59000 as in CI): {e}"
))
})?;
Ok(store)
Expand Down
4 changes: 3 additions & 1 deletion app/tests/golden/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ static NOISE: LazyLock<Vec<(&'static str, Regex)>> = LazyLock::new(|| {
fn normalize(value: &serde_json::Value) -> serde_json::Value {
let mut seen: HashMap<(&'static str, String), usize> = HashMap::new();
let mut counts: HashMap<&'static str, usize> = HashMap::new();
// The blob store's address, as CI's: VISUALCI_APP_TEST_S3_ENDPOINT moves it to another port on a build box.
let endpoint = app::testing::s3_endpoint();
let mut fix = |s: &str| -> String {
let mut out = s.to_string();
let mut out = s.replace(&endpoint, "http://127.0.0.1:59000");
for &(kind, ref re) in NOISE.iter() {
out = re
.replace_all(&out, |caps: &regex::Captures| {
Expand Down
2 changes: 1 addition & 1 deletion app/tests/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn in_production_with_no_sign_ups_setting() -> laravel::foundation::ApplicationB
("QUEUE_CONNECTION", "sync"),
("DB_CONNECTION", "pgsql"),
("DATABASE_URL", app::testing::database_url().as_str()),
("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"),
("VISUALCI_S3_ENDPOINT", app::testing::s3_endpoint().as_str()),
("VISUALCI_S3_BUCKET", "visualci-test"),
("VISUALCI_S3_ACCESS_KEY_ID", "visualci"),
("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,6 @@ Never set these in production.
| --- | --- | --- | --- |
| `VISUALCI_NOW` | string | | Stands in for the clock in credit allowances (RFC 3339), for screenshots of seeded data. |
| `VISUALCI_APP_TEST_DATABASE_URL` | string | | The tests' Postgres (each test borrows a copy of it). Default: CI's, `postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test`. |
| `VISUALCI_APP_TEST_S3_ENDPOINT` | string | | The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`. |
| `VISUALCI_EMAIL_SNAPSHOTS` | string | | A folder the email tests write each email's HTML to, for review. |
| `VISUALCI_GOLDEN_UPDATE` | string | | `1` makes the tests write their golden files (this page among them) instead of comparing. |
80 changes: 68 additions & 12 deletions scripts/on-dev
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
# the command builds on the line whose slot name is the member the pool gave: docspring-dev first, a rented box only
# when its queue is long. Run inside a lease you took yourself (`fleetlayer lease run docspring-dev-light -- ...`), it
# builds on the line with that lease's slot name. A `run` that uses the test database (one naming 127.0.0.1:55433 or
# DATABASE_URL, such as app/'s tests with VISUALCI_APP_TEST_DATABASE_URL) always leases the first line's slot instead of
# the pool's: only that host has it.
# DATABASE_URL) builds on the first line's host, which has the test database (docspring-dev's Postgres on 55433), unless
# it's app/'s tests (one naming VISUALCI_APP_TEST_DATABASE_URL): those build anywhere, and on any other host start their
# own Postgres and RustFS in Docker on free ports, as CI does, point the command's database URL and
# VISUALCI_APP_TEST_S3_ENDPOINT at them, and remove them when they end (Ctrl-C included).
set -euo pipefail
remote_root=/opt/code/visualci/checkouts
dir=$(git rev-parse --show-toplevel)
Expand All @@ -33,11 +35,11 @@ hosts=$(grep -v '^[[:space:]]*\(#\|$\)' "$dir/.dev-host" 2>/dev/null || true)
pool=$(printf '%s\n' "$hosts" | awk '$1 == "pool" {print $2; exit}')
hosts=$(printf '%s\n' "$hosts" | awk 'NF && $1 != "pool"')
[ -n "$hosts" ] || hosts=docspring-dev.hs
# app/'s tests need the test database, which only the first host has (docspring-dev's Postgres on 127.0.0.1:55433):
# they lease that host's slot rather than the pool's, which may hand out a rented box without one (every database test then
# fails with PoolTimedOut).
# The first host has the test database (docspring-dev's Postgres on 127.0.0.1:55433, MinIO on 59000). app/'s tests get
# throwaway containers anywhere else; other database runs (the screenshots, journeys, a seeded dev server) need that host.
db_line=$(printf '%s\n' "$hosts" | head -n 1)
db_kind=$(printf '%s\n' "$db_line" | awk '{print $2}')
db_host=$(printf '%s\n' "$db_line" | awk '{print $1}')
# Inside a lease: the line whose slot name is the lease's kind (its light twin builds on the same host).
kind=${FLEETLAYER_LEASE_KIND:-}
line=
Expand All @@ -46,28 +48,33 @@ line=
host=${ON_DEV_HOST:-$(printf '%s\n' "$line" | awk '{print $1}')}

die() { echo "scripts/on-dev: $*" >&2; exit 1; }
usage() { sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
usage() { sed -n '2,27p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
[ $# -gt 0 ] || usage
cmd=$1; shift

# Whether the command uses the test database: a `run` that names it.
# Whether the command uses the test database (a `run` that names it), and whether it's app/'s tests, which can have their
# own.
needs_db=
app_tests=
case $cmd in
run) case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac ;;
run)
case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac
case " $* " in *VISUALCI_APP_TEST_DATABASE_URL=*) app_tests=1 ;; esac
;;
esac

# With a pool and no lease held yet: hold a slot from the pool for the whole command, and run this script again inside
# it, where the pool's member picks the host. sync and clean only copy or delete files.
case $cmd in sync | clean) ;; *)
if [ -n "$pool" ] && [ -z "$kind" ] && [ -z "${ON_DEV_HOST:-}" ]; then
case $cmd in fmt | check | clippy) pool_kind=$pool-light ;; *) pool_kind=$pool ;; esac
[ -n "$needs_db" ] && [ -n "$db_kind" ] && pool_kind=$db_kind
[ -n "$needs_db" ] && [ -z "$app_tests" ] && [ -n "$db_kind" ] && pool_kind=$db_kind
exec fleetlayer lease run "$pool_kind" --reason "visualci on-dev $cmd ($name)" -- "$0" "$cmd" "$@"
fi ;;
esac

if [ -n "$needs_db" ] && [ -z "${ON_DEV_HOST:-}" ] && [ "$line" != "$db_line" ]; then
die "$cmd needs the test database, which is on $(printf '%s\n' "$db_line" | awk '{print $1}'), but this lease ($kind) builds on $host: take a ${db_kind:-docspring-dev} lease, or run it outside one"
if [ -n "$needs_db" ] && [ -z "$app_tests" ] && [ "$host" != "$db_host" ]; then
die "$cmd needs the test database, which is on $db_host, but it would build on $host: take a ${db_kind:-docspring-dev} lease, or run it outside one (only app/'s tests, naming VISUALCI_APP_TEST_DATABASE_URL, get their own)"
fi

sync() {
Expand Down Expand Up @@ -99,6 +106,42 @@ run() {
return "$status"
}

# A database run on a host without the test database: its own Postgres and RustFS (CI's images and credentials), named
# for this run so parallel runs on one box don't meet, on ports Docker picks, removed when this script exits.
services() {
local tag
tag=vci-$(printf '%s' "$name" | tr -c 'a-zA-Z0-9_.-' '-')-$$-$RANDOM
containers="$tag-pg $tag-s3"
trap 'ssh "$host" "docker rm -f $containers >/dev/null 2>&1" || true' EXIT
trap 'exit 130' INT TERM
# Each carries when it expires (6 hours on): a run removes earlier runs' that outlived it (a script killed with
# SIGKILL, or a box its cleanup couldn't reach).
local ports
ports=$(ssh "$host" "set -e
now=\$(date +%s)
for c in \$(docker ps -aq --filter label=visualci-on-dev-expires); do
[ \"\$(docker inspect -f '{{index .Config.Labels \"visualci-on-dev-expires\"}}' \$c)\" -lt \$now ] && docker rm -f \$c >/dev/null || true
done
label=visualci-on-dev-expires=\$((now + 21600))
docker run -d --rm --name $tag-pg --label \$label -p 127.0.0.1::5432 -e POSTGRES_USER=visualci \
-e POSTGRES_PASSWORD=visualci -e POSTGRES_DB=visualci_app_test postgres:17 >/dev/null
docker run -d --rm --name $tag-s3 --label \$label -p 127.0.0.1::9000 -e RUSTFS_ACCESS_KEY=visualci \
-e RUSTFS_SECRET_KEY=visualci-dev-secret rustfs/rustfs:1.0.6 >/dev/null
pg=\$(docker port $tag-pg 5432/tcp | head -n1 | sed 's/.*://'); s3=\$(docker port $tag-s3 9000/tcp | head -n1 | sed 's/.*://')
[ -n \"\$pg\" ] && [ -n \"\$s3\" ] || { echo 'a container stopped as it started' >&2; exit 1; }
ready=
for _ in \$(seq 1 60); do docker exec $tag-pg pg_isready -q -U visualci -h 127.0.0.1 && ready=1 && break; sleep 1; done
[ -n \"\$ready\" ] || { echo 'Postgres not ready after 60 s' >&2; exit 1; }
ready=
for _ in \$(seq 1 60); do curl -s -o /dev/null http://127.0.0.1:\$s3/ && ready=1 && break; sleep 1; done
[ -n \"\$ready\" ] || { echo 'RustFS not answering after 60 s' >&2; exit 1; }
echo \$pg \$s3") || die "couldn't start the test Postgres and RustFS in Docker on $host"
local pg=${ports% *} s3=${ports#* }
db_url=postgres://visualci:visualci@127.0.0.1:$pg/visualci_app_test
s3_url=http://127.0.0.1:$s3
echo "scripts/on-dev: test Postgres on 127.0.0.1:$pg and RustFS on 127.0.0.1:$s3 on $host" >&2
}

case $cmd in
sync) sync ;;
fmt) sync; run cargo fmt --all --check ;;
Expand All @@ -113,7 +156,20 @@ case $cmd in
*) run cargo test -q --locked -j 24 --workspace --exclude visualci-app "$@" ;;
esac
;;
run) sync; run "$@" ;;
run)
sync
if [ -n "$app_tests" ] && [ "$host" != "$db_host" ]; then
services
# The command's own database URL (docspring-dev's, on 55433), whole, becomes the container's
args=()
for a in "$@"; do
args+=("$(printf '%s' "$a" | sed -E "s#postgres(ql)?://[^[:space:]'\"]*(127\.0\.0\.1|localhost|\[::1\]):55433[^[:space:]'\"]*#$db_url#g")")
done
run env VISUALCI_APP_TEST_DATABASE_URL="$db_url" VISUALCI_APP_TEST_S3_ENDPOINT="$s3_url" "${args[@]}"
else
run "$@"
fi
;;
fix)
sync
run cargo clippy -q --locked -j 24 --workspace --all-targets --fix --allow-dirty --allow-no-vcs "$@" || true
Expand Down
Loading