test(previews): unit-test the dev-mobile helpers - #725
test(previews): unit-test the dev-mobile helpers#725Barnabas A Nsoh (ayinloya) wants to merge 2 commits into
Conversation
Mirrors smileidentity/web-sdk#71, keeping the two copies of this script in step. The bug fixed in #724 — an ERE alternation that anchored only its first branch, so ordinary app log lines aborted a healthy deploy — was pure string matching. It shipped in #651 and passed review, which is a good argument for covering this code. dev-mobile.sh could not be tested as written: its top-level code runs port preflight, `mktemp -d` and eventually a full build, tunnel and `sst dev` the moment the file is read, so sourcing it to reach a function is not an option. Move the pure helpers into scripts/lib/dev-mobile-lib.sh, which is side-effect free to source, and have the script source it. Extracted: env_file_default (now takes the file as an argument rather than reading $PREVIEWS_DIR), is_valid_port, extract_tunnel_url, sst_log_plain (likewise now takes a path), sst_log_is_ready, sst_log_has_error. The last two were previously inline greps. Behaviour is unchanged. Adds 14 node --test cases covering the regexes, the env-file parsing and port validation, matching the .test.mjs convention already used in packages/. Deliberately not covered: tunnels, the sst readiness loop and process-group cleanup — mocking cloudflared and sst would cost more than it protects. Wires `npm run test` into a previews job in lint.yml. The repo's existing node --test suites are not run by any workflow; this one is, otherwise it would not defend against the regression that prompted it.
🔍 Semgrep Security Scan Results✅ No security findings detected by |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨No code suggestions found for the PR. |
|
This branch has been deployed to s3 / cloudfront. ✅ Preview URL for Smart Camera Web: ✅ Preview URL for Embed: ✅ Preview URL for Web Client (Sandbox): ✅ Preview URL for Web Client (Production): |
…ntain permissions' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
User description
Mirrors the same change made to our v3 SDK repo's copy of this script, so the two stay in step.
Why
The bug fixed in #724 — an ERE alternation that anchored only its first branch, so an ordinary app log line like
user does not existaborted a healthy deploy — was pure string matching. No AWS, no processes, no timing. It shipped in #651 and passed review. That's the case for covering this code.Two more helpers carry the same risk profile:
env_file_default(parsesexport K = "v", strips quotes and whitespace, last-wins) andsst_log_plain(a sed pipeline stripping ANSI escapes and turning CRs into newlines).The refactor this needed
dev-mobile.shcould not be tested as written. Its top-level code runs port preflight,mktemp -d, and eventually a full build + tunnel +sst devthe moment the file is read — so sourcing it to reach a function executes all of that.The pure helpers now live in
previews/scripts/lib/dev-mobile-lib.sh, which is side-effect free to source;dev-mobile.shsources it. Extracted:env_file_default$PREVIEWS_DIRsst_log_plain$SST_SERVER_LOGis_valid_port[[ =~ ]]testextract_tunnel_urlsst_log_is_readysst_log_has_errorBehaviour is unchanged.
The tests
14 cases under
node --test, matching the.test.mjsconvention already used acrosspackages/. No new dependency, no test framework. They shell into bash with the library sourced, passing fixtures as positional args so no test data has to survive shell quoting.Not covered, deliberately: tunnel orchestration, the sst readiness loop, process-group cleanup. Mocking
cloudflared,sstandlsofwould cost more than it protects.Verified
The suite was mutation-tested against three deliberate regressions, all caught: restoring the original unanchored alternation, dropping the
$anchor from theCompletereadiness check, and makingenv_file_defaultfirst-wins instead of last-wins.Checked here:
bash -non both files;npm run test(14/14) andnpm run lintinpreviews; prettier clean; and the script still behaves identically — non-numeric port rejected,previews/.envstill read.CI
Adds a
previewsjob tolint.ymlrunningnpm run lintandnpm run test.Worth flagging: the repo's existing
node --testsuites inpackages/are not run by any workflow — I checked all of them. I wired this one up because a test that never runs wouldn't defend against the regression that prompted it. Whether to wire up the others is a separate question, and probably worth someone's attention.Parity
After this, the two repos' copies of
dev-mobile.shdiffer only by the workspace name they build (@smileid/web-componentshere), anddev-mobile-lib.sh/dev-mobile.test.mjsare byte-identical across both.PR Type
Tests, Enhancement
Description
Extract pure
dev-mobile.shhelpers into sourceablelib/dev-mobile-lib.shAdd 14
node --testcases covering regexes, env parsing, portsWire a
previewslint + unit-test job into CIAdd
testscript topreviews/package.jsonDiagram Walkthrough
File Walkthrough
dev-mobile-lib.sh
New sourceable library of pure dev-mobile helperspreviews/scripts/lib/dev-mobile-lib.sh
env_file_default(now takes file arg),is_valid_port,extract_tunnel_url,sst_log_plain(now takes path),sst_log_is_ready,sst_log_has_error.error regex must stay fully anchored.
dev-mobile.sh
Delegate string helpers to extracted librarypreviews/scripts/dev-mobile.sh
SCRIPT_DIRand sourceslib/dev-mobile-lib.sh.env_file_defaultandsst_log_plaindefinitions; callsthem with explicit path arguments.
is_valid_port,extract_tunnel_url,sst_log_is_ready,sst_log_has_error.dev-mobile.test.mjs
Unit tests for dev-mobile shell helperspreviews/scripts/dev-mobile.test.mjs
node --testcases exercising the extracted helpers via bash.errors must not abort.
(last-wins, prefix keys, comments), port validation and tunnel URL
extraction.
avoid shell quoting issues.
lint.yml
Add previews lint and unit-test CI job.github/workflows/lint.yml
previewsjob running in./previews.npm ci,npm run lintandnpm run test.package.json
Add test script for previews packagepreviews/package.json
testscript runningnode --test scripts/*.test.mjs.