Compare commits

..

4 Commits

Author SHA1 Message Date
mihir 2992c9e8f2 fix: require backend JSON readiness before activating a release
continuous-integration/drone/push Build is failing Details
2026-11-20 18:07:55 +05:30
mihir f4dd351f87 fix: prepare the existing HTTP site for SSH releases
continuous-integration/drone/push Build is passing Details
2026-11-20 18:00:25 +05:30
mihir 6bd1940f5e test: make sample approval and dispatch fixtures deterministic 2026-11-20 18:00:25 +05:30
mihir cdcf0d9898 test: reduce fixture typing overhead in retry workflows 2026-11-20 17:59:04 +05:30
6 changed files with 111 additions and 53 deletions

View File

@ -6,42 +6,33 @@ script, and activates the release through SSH. The sole deployment secret is
## One-time server setup ## One-time server setup
Obtain this branch's files on server1 using your existing repository access, Server inspection on 19 November 2026 found an HTTP-only site at
or transfer the Nginx snippet from your workstation. From the repository directory, `/etc/nginx/sites-enabled/ManiCandles.midastix.com.conf`, with its old root at
run the following as `mihir`. `/var/www/ManiCandles.midastix.com`. Certbot is installed. The setup script is
specific to that configuration and refuses to overwrite a site with TLS settings.
Obtain this branch's files on server1 using your existing repository access.
From the repository directory, run the following as `mihir`.
```sh ```sh
sudo install -d -o mihir -g "$(id -gn mihir)" -m 0755 \ sudo sh scripts/setup-static-host.sh
/var/www/manicandles /var/www/manicandles/releases /var/www/manicandles/incoming sudo certbot --nginx -d manicandles.midastix.com --redirect
sudo install -d -m 0755 /etc/nginx/snippets sudo nginx -t
sudo install -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf curl --fail --show-error https://manicandles.midastix.com/
command -v flock
command -v curl
``` ```
Identify the existing Nginx site file for `manicandles.midastix.com`. Alternatively, copy `scripts/setup-static-host.sh` and
`deploy/nginx-locations.conf` together to `/tmp/` on server1, and run
`sudo sh /tmp/setup-static-host.sh`, followed by the Certbot and verification
commands above. The snippet must be beside the setup script for this layout.
```sh Setup backs up the original site under `/var/backups`, prepares writable release
sudo nginx -T 2>&1 | grep -E '^# configuration file|server_name.*manicandles' directories, installs the snippet, validates Nginx, and reloads it. Validation or
``` reload failure restores the saved site file. If the old root contains an index,
setup retains it as the initial `current` release and a rollback target. If no
Back up that site file before editing it. Inside its existing HTTPS `server` old index exists, the frontend awaits its first deployment. DNS must point to
block for `manicandles.midastix.com`, replace the old `root`, `index`, and server1 and public port 80 must be reachable for certificate issuance. Complete
`location` directives with this include. Preserve its `listen`, `server_name`, Certbot's email and agreement prompts yourself. Verify HTTPS before merging.
certificate paths, TLS settings, and HTTP-to-HTTPS redirect. Resolve any duplicate
security header definitions against the snippet.
```nginx
include /etc/nginx/snippets/manicandles.conf;
```
Validate and reload. If validation fails, restore the saved site file before
continuing. Reload preserves existing connections. The frontend will begin
serving when the first release supplies `/var/www/manicandles/current`.
```sh
sudo nginx -t && sudo systemctl reload nginx
```
Save the complete matching private key in Drone's `manicandles_deploy_key` Save the complete matching private key in Drone's `manicandles_deploy_key`
secret. Its public key must be authorized for `mihir` on server1. Keep the secret. Its public key must be authorized for `mihir` on server1. Keep the
@ -59,7 +50,8 @@ overwrite a published release. A file lock
serializes activation. An older build cannot replace a newer active build. serializes activation. An older build cannot replace a newer active build.
The `current` symlink changes atomically. HTTPS checks use the real hostname The `current` symlink changes atomically. HTTPS checks use the real hostname
resolved to loopback and verify the certificate, release marker, homepage, resolved to loopback and verify the certificate, release marker, homepage,
`/workspace`, `/cart`, and `/api/v1/health/ready`. Failure restores the previous `/workspace`, `/cart`, and `/api/v1/health/ready`. Readiness must return the backend's
`{"status":"ok"}` response. An HTML fallback fails verification. Failure restores the previous
current symlink, or removes the first activation if no release existed before it. current symlink, or removes the first activation if no release existed before it.
The last successful release is also retained at `previous`. Its assets can serve The last successful release is also retained at `previous`. Its assets can serve
requests from browser tabs opened before the update. Failed first deployments requests from browser tabs opened before the update. Failed first deployments

View File

@ -25,7 +25,7 @@ test "$(cat "$release/release.txt")" = "$release_id"
# A slow older pipeline must not replace a newer successful release. # A slow older pipeline must not replace a newer successful release.
last_build=0 last_build=0
if [ -L current ]; then if [ -L current ] && [ -f current/release.txt ]; then
last_release=$(cat current/release.txt) last_release=$(cat current/release.txt)
last_build=${last_release%%-*} last_build=${last_release%%-*}
fi fi
@ -80,8 +80,11 @@ mv -Tf "$temporary" current
served=$(curl --fail --silent --show-error --max-time 20 \ served=$(curl --fail --silent --show-error --max-time 20 \
--resolve "$host:443:127.0.0.1" "https://$host/release.txt") --resolve "$host:443:127.0.0.1" "https://$host/release.txt")
test "$served" = "$release_id" test "$served" = "$release_id"
for path in / /workspace /cart /api/v1/health/ready; do for path in / /workspace /cart; do
curl --fail --silent --show-error --output /dev/null --max-time 20 \ curl --fail --silent --show-error --output /dev/null --max-time 20 \
--resolve "$host:443:127.0.0.1" "https://$host$path" --resolve "$host:443:127.0.0.1" "https://$host$path"
done done
ready=$(curl --fail --silent --show-error --max-time 20 \
--resolve "$host:443:127.0.0.1" "https://$host/api/v1/health/ready")
test "$(printf '%s' "$ready" | tr -d '[:space:]')" = '{"status":"ok"}'
echo "Activated $release_id on $host" echo "Activated $release_id on $host"

View File

@ -0,0 +1,46 @@
#!/bin/sh
set -eu
test "$(id -u)" = 0 || { echo 'Run this setup script with sudo.' >&2; exit 1; }
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd)
snippet="$script_dir/../deploy/nginx-locations.conf"
if [ ! -f "$snippet" ]; then snippet="$script_dir/nginx-locations.conf"; fi
test -s "$snippet"
site=$(readlink -f /etc/nginx/sites-enabled/ManiCandles.midastix.com.conf)
test -f "$site"
if grep -Eq 'listen[[:space:]].*443|ssl_certificate' "$site"; then
echo 'This site already contains TLS settings. Review its configuration before running setup.' >&2
exit 1
fi
nginx -t
command -v curl
command -v flock
command -v certbot
root=/var/www/manicandles
install -d -o mihir -g "$(id -gn mihir)" -m 0755 "$root" "$root/releases" "$root/incoming"
if [ ! -e "$root/current" ] && [ ! -L "$root/current" ] && [ -s /var/www/ManiCandles.midastix.com/index.html ]; then
ln -s /var/www/ManiCandles.midastix.com "$root/current"
chown -h mihir:"$(id -gn mihir)" "$root/current"
fi
install -d -m 0755 /etc/nginx/snippets /var/backups
backup="/var/backups/manicandles-nginx-$(date +%Y%m%d%H%M%S)-$$.conf"
cp -p "$site" "$backup"
if [ -e /etc/nginx/snippets/manicandles.conf ] && ! cmp -s "$snippet" /etc/nginx/snippets/manicandles.conf; then
echo 'A different deployment snippet exists. Review the existing setup before continuing.' >&2
exit 1
fi
install -m 0644 "$snippet" /etc/nginx/snippets/manicandles.conf
cat > "$site" <<'NGINX'
server {
listen 80;
server_name manicandles.midastix.com;
include /etc/nginx/snippets/manicandles.conf;
}
NGINX
if ! nginx -t || ! systemctl reload nginx; then
cp -p "$backup" "$site"
nginx -t && systemctl reload nginx
echo "Setup failed. Restored $backup" >&2
exit 1
fi
echo "Site prepared. Original configuration saved at $backup"
echo 'Next run: sudo certbot --nginx -d manicandles.midastix.com --redirect'

View File

@ -15,11 +15,17 @@ case "$*" in
else else
cat "$DEPLOY_TEST_ROOT/current/release.txt" cat "$DEPLOY_TEST_ROOT/current/release.txt"
fi ;; fi ;;
*health/ready*) test ! -f "$DEPLOY_TEST_ROOT/fail-health" ;; *health/ready*)
test ! -f "$DEPLOY_TEST_ROOT/fail-health" || exit 1
if [ -f "$DEPLOY_TEST_ROOT/html-readiness" ]; then
echo '<html>Page not found</html>'
else
echo '{"status":"ok"}'
fi ;;
esac esac
MOCK MOCK
chmod +x "$fixture/bin/curl" chmod +x "$fixture/bin/curl"
for release in 1-aaa 2-bbb 3-ccc; do for release in 1-aaa 2-bbb 3-ccc 4-ddd; do
mkdir -p "$fixture/site/incoming/$release/dist" mkdir -p "$fixture/site/incoming/$release/dist"
printf '%s\n' "$release" > "$fixture/site/incoming/$release/dist/release.txt" printf '%s\n' "$release" > "$fixture/site/incoming/$release/dist/release.txt"
printf '<html>Test</html>\n' > "$fixture/site/incoming/$release/dist/index.html" printf '<html>Test</html>\n' > "$fixture/site/incoming/$release/dist/index.html"
@ -51,4 +57,18 @@ test "$(cat "$fixture/site/current/release.txt")" = 2-bbb
rm "$fixture/site/wrong-marker" rm "$fixture/site/wrong-marker"
deploy 1-aaa deploy 1-aaa
test "$(cat "$fixture/site/current/release.txt")" = 2-bbb test "$(cat "$fixture/site/current/release.txt")" = 2-bbb
# The existing HTTP site has no release metadata and must remain a rollback target.
rm "$fixture/site/current"
mkdir -p "$fixture/site/legacy"
echo legacy > "$fixture/site/legacy/index.html"
ln -s "$fixture/site/legacy" "$fixture/site/current"
touch "$fixture/site/fail-health"
if deploy 3-ccc; then echo 'Expected rollback to legacy site' >&2; exit 1; fi
test "$(cat "$fixture/site/current/index.html")" = legacy
rm "$fixture/site/fail-health"
deploy 3-ccc
test "$(cat "$fixture/site/previous/index.html")" = legacy
touch "$fixture/site/html-readiness"
if deploy 4-ddd; then echo 'Expected rollback when readiness serves HTML' >&2; exit 1; fi
test "$(cat "$fixture/site/current/release.txt")" = 3-ccc
printf 'Deployment activation, rollback and stale-build checks passed.\n' printf 'Deployment activation, rollback and stale-build checks passed.\n'

View File

@ -1,5 +1,5 @@
import { type ReactNode } from "react"; import { type ReactNode } from "react";
import { render, screen } from "@testing-library/react"; import { fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ThemeProvider } from "@mui/material/styles"; import { ThemeProvider } from "@mui/material/styles";
@ -92,11 +92,9 @@ test("an uncertain sample request retries with its original account, variant, qu
const user = userEvent.setup(); const user = userEvent.setup();
await user.click(screen.getByRole("button", { name: "Test login" })); await user.click(screen.getByRole("button", { name: "Test login" }));
await user.type(await screen.findByRole("textbox", { name: /Variant ID/ }), variantId); fireEvent.change(await screen.findByRole("textbox", { name: /Variant ID/ }), { target: { value: variantId } });
const quantity = screen.getByRole("spinbutton", { name: /Sample quantity/ }); fireEvent.change(screen.getByRole("spinbutton", { name: /Sample quantity/ }), { target: { value: "4" } });
await user.clear(quantity); fireEvent.change(screen.getByRole("textbox", { name: /Sample purpose/ }), { target: { value: sample.purpose } });
await user.type(quantity, "4");
await user.type(screen.getByRole("textbox", { name: /Sample purpose/ }), sample.purpose);
await user.click(screen.getByRole("button", { name: "Request samples" })); await user.click(screen.getByRole("button", { name: "Request samples" }));
await user.click(await screen.findByRole("button", { name: "Retry sample request" })); await user.click(await screen.findByRole("button", { name: "Retry sample request" }));
await screen.findByText(sample.purpose); await screen.findByText(sample.purpose);
@ -157,10 +155,11 @@ test("an independent reviewer can approve a request and retry a manual dispatch"
const user = userEvent.setup(); const user = userEvent.setup();
await user.click(screen.getByRole("button", { name: "Test login" })); await user.click(screen.getByRole("button", { name: "Test login" }));
await user.selectOptions(await screen.findByRole("combobox", { name: /Review decision/ }), "APPROVED"); await user.selectOptions(await screen.findByRole("combobox", { name: /Review decision/ }), "APPROVED");
await user.type(screen.getByRole("textbox", { name: /Review note/ }), approved.note); fireEvent.change(screen.getByRole("textbox", { name: /Review note/ }), { target: { value: approved.note } });
await user.click(screen.getByRole("button", { name: "Record sample decision" })); await user.click(screen.getByRole("button", { name: "Record sample decision" }));
await user.type(await screen.findByRole("textbox", { name: /Dispatch reference/ }), dispatched.dispatchReference); expect(decision).toEqual({ decision: "APPROVED", note: approved.note, idempotencyKey: expect.any(String) });
await user.type(screen.getByRole("textbox", { name: /Dispatch note/ }), dispatched.note); fireEvent.change(await screen.findByRole("textbox", { name: /Dispatch reference/ }), { target: { value: dispatched.dispatchReference } });
fireEvent.change(screen.getByRole("textbox", { name: /Dispatch note/ }), { target: { value: dispatched.note } });
await user.click(screen.getByRole("button", { name: "Record dispatch" })); await user.click(screen.getByRole("button", { name: "Record dispatch" }));
await user.click(await screen.findByRole("button", { name: "Retry dispatch" })); await user.click(await screen.findByRole("button", { name: "Retry dispatch" }));
await screen.findByRole("cell", { name: dispatched.dispatchReference }); await screen.findByRole("cell", { name: dispatched.dispatchReference });

View File

@ -1,5 +1,5 @@
import { type ReactNode } from "react"; import { type ReactNode } from "react";
import { act, render, screen, waitFor, within } from "@testing-library/react"; import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { Table } from "../src/components/WorkspaceTools"; import { Table } from "../src/components/WorkspaceTools";
import { ConfirmAction } from "../src/components/ConfirmAction"; import { ConfirmAction } from "../src/components/ConfirmAction";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
@ -722,19 +722,17 @@ test("a production batch starts, safely retries usage and completes with reconci
await user.click(await screen.findByRole("button", { name: "Start batch" })); await user.click(await screen.findByRole("button", { name: "Start batch" }));
await user.click(screen.getByRole("button", { name: "Confirm" })); await user.click(screen.getByRole("button", { name: "Confirm" }));
await user.selectOptions(await screen.findByRole("combobox", { name: /Usage type/ }), "WASTED"); await user.selectOptions(await screen.findByRole("combobox", { name: /Usage type/ }), "WASTED");
await user.type(screen.getByRole("textbox", { name: /Actual quantity/ }), "0.125"); fireEvent.change(screen.getByRole("textbox", { name: /Actual quantity/ }), { target: { value: "0.125" } });
await user.type(screen.getByRole("textbox", { name: /Usage reason/ }), "Spill"); fireEvent.change(screen.getByRole("textbox", { name: /Usage reason/ }), { target: { value: "Spill" } });
await user.click(screen.getByRole("button", { name: "Record material usage" })); await user.click(screen.getByRole("button", { name: "Record material usage" }));
await user.click(await screen.findByRole("button", { name: "Retry material usage" })); await user.click(await screen.findByRole("button", { name: "Retry material usage" }));
await screen.findByRole("cell", { name: "199.875" }); await screen.findByRole("cell", { name: "199.875" });
expect(attempts).toHaveLength(2); expect(attempts).toHaveLength(2);
expect(attempts[0]).toEqual({ bomLineId: id, kind: "WASTED", quantity: "0.125", reason: "Spill", idempotencyKey: expect.any(String) }); expect(attempts[0]).toEqual({ bomLineId: id, kind: "WASTED", quantity: "0.125", reason: "Spill", idempotencyKey: expect.any(String) });
expect(attempts[1]).toEqual(attempts[0]); expect(attempts[1]).toEqual(attempts[0]);
await user.type(screen.getByRole("spinbutton", { name: /Good pieces/ }), "18"); fireEvent.change(screen.getByRole("spinbutton", { name: /Good pieces/ }), { target: { value: "18" } });
const rejected = screen.getByRole("spinbutton", { name: /Rejected pieces/ }); fireEvent.change(screen.getByRole("spinbutton", { name: /Rejected pieces/ }), { target: { value: "2" } });
await user.clear(rejected); fireEvent.change(screen.getByRole("textbox", { name: /Rejection reason/ }), { target: { value: "Surface defects" } });
await user.type(rejected, "2");
await user.type(screen.getByRole("textbox", { name: /Rejection reason/ }), "Surface defects");
await user.click(screen.getByRole("button", { name: "Complete batch" })); await user.click(screen.getByRole("button", { name: "Complete batch" }));
await screen.findByText(/18 good · 2 rejected · Surface defects/); await screen.findByText(/18 good · 2 rejected · Surface defects/);
expect(screen.queryByRole("button", { name: "Record material usage" })).toBeNull(); expect(screen.queryByRole("button", { name: "Record material usage" })).toBeNull();