socket-patch-cli 3.3.0

CLI binary for socket-patch: apply, rollback, get, scan security patches
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Docker-driven full install→apply chain for the pypi ecosystem.
//!
//! Real `pip install six==1.16.0` (single-file package — small, stable,
//! easy to verify) in a Linux container, then `socket-patch scan
//! --json --sync --yes` against a wiremock-served patch fixture, then
//! `socket-patch apply --json --force --offline` overwrites the real
//! installed `site-packages/six.py` with synthetic bytes containing
//! `SOCKET-PATCH-E2E-MARKER`. The grep at the end of the container
//! script is the gate.
//!
//! Two test functions:
//! - `pypi_local_install_full_apply_chain` — venv install at
//!   `.venv/lib/python3.X/site-packages/six.py`
//! - `pypi_global_install_full_apply_chain` — `pip install
//!   --break-system-packages` to system site-packages; socket-patch
//!   scan + apply with `--global`

#![cfg(feature = "docker-e2e")]

use std::process::Command;

use base64::Engine;
use sha2::{Digest, Sha256};
use wiremock::matchers::{method, path, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};

const ORG: &str = "test-org";
const PURL: &str = "pkg:pypi/six@1.16.0";
const UUID: &str = "12121212-1212-4121-8121-121212121212";

/// The synthetic content that replaces the installed six.py file.
/// Contains the marker we grep for to verify apply succeeded.
const PATCHED_PY: &[u8] = b"# SOCKET-PATCH-E2E-MARKER\n\
                            # six.py replaced by socket-patch e2e fixture\n\
                            __version__ = \"1.16.0-patched\"\n";

/// Coverage instrumentation hook. The CI coverage-docker job sets
/// SOCKET_PATCH_COV_BIN (host path to an llvm-cov-instrumented
/// socket-patch binary) and SOCKET_PATCH_COV_PROFRAW_DIR (host dir
/// for in-container *.profraw output). When both are set, the docker
/// run mounts the instrumented binary over the image's baked-in
/// /usr/local/bin/socket-patch and points LLVM_PROFILE_FILE into a
/// host-visible volume so in-container code paths contribute to the
/// host's lcov merge. Empty Vec when unset.
fn cov_docker_args() -> Vec<String> {
    let Ok(bin) = std::env::var("SOCKET_PATCH_COV_BIN") else {
        return Vec::new();
    };
    let Ok(dir) = std::env::var("SOCKET_PATCH_COV_PROFRAW_DIR") else {
        return Vec::new();
    };
    vec![
        "-v".into(),
        format!("{bin}:/usr/local/bin/socket-patch:ro"),
        "-v".into(),
        format!("{dir}:/coverage"),
        "-e".into(),
        "LLVM_PROFILE_FILE=/coverage/docker-e2e-%p-%14m.profraw".into(),
    ]
}

fn git_sha256(content: &[u8]) -> String {
    let header = format!("blob {}\0", content.len());
    let mut hasher = Sha256::new();
    hasher.update(header.as_bytes());
    hasher.update(content);
    hex::encode(hasher.finalize())
}

async fn make_mock_server(after_hash: &str) -> MockServer {
    let listener =
        std::net::TcpListener::bind("0.0.0.0:0").expect("bind wiremock to 0.0.0.0:0");
    let server = MockServer::builder().listener(listener).start().await;

    // 1. Batch search reports a patch for the installed PURL.
    Mock::given(method("POST"))
        .and(path(format!("/v0/orgs/{ORG}/patches/batch")))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "packages": [{
                "purl": PURL,
                "patches": [{
                    "uuid": UUID, "purl": PURL,
                    "tier": "free", "cveIds": [], "ghsaIds": [],
                    "severity": "high", "title": "pypi e2e fixture"
                }]
            }],
            "canAccessPaidPatches": false,
        })))
        .mount(&server)
        .await;

    // 2. By-package lookup (used by scan --apply / --sync).
    Mock::given(method("GET"))
        .and(path_regex(format!(
            "^/v0/orgs/{ORG}/patches/by-package/.+$"
        )))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "patches": [{
                "uuid": UUID, "purl": PURL,
                "publishedAt": "2024-01-01T00:00:00Z",
                "description": "pypi e2e fixture",
                "license": "MIT", "tier": "free",
                "vulnerabilities": {}
            }],
            "canAccessPaidPatches": false,
        })))
        .mount(&server)
        .await;

    // 3. Full patch view with inline blobContent. The pypi file-path
    // convention is `<pkg_name>/<rel>` with NO `package/` prefix —
    // unique to pypi because the crawler returns site-packages root
    // as pkg_path. For single-file six.py, the path is just "six.py".
    let blob_b64 = base64::engine::general_purpose::STANDARD.encode(PATCHED_PY);
    Mock::given(method("GET"))
        .and(path(format!("/v0/orgs/{ORG}/patches/view/{UUID}")))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "uuid": UUID,
            "purl": PURL,
            "publishedAt": "2024-01-01T00:00:00Z",
            "files": {
                "six.py": {
                    "beforeHash": "0000000000000000000000000000000000000000000000000000000000000000",
                    "afterHash":  after_hash,
                    "blobContent": blob_b64,
                }
            },
            "vulnerabilities": {},
            "description": "pypi e2e fixture",
            "license": "MIT",
            "tier": "free",
        })))
        .mount(&server)
        .await;

    server
}

fn local_script(api_url: &str) -> String {
    format!(
        r#"#!/usr/bin/env bash
set -uo pipefail

# 1. Real local install: venv + pip install. six is a single-file
#    pypi package — installs to site-packages/six.py.
python3 -m venv /workspace/venv
. /workspace/venv/bin/activate
pip install --disable-pip-version-check --quiet --no-cache-dir six==1.16.0

# Link the venv into the cwd so the python crawler discovers it.
mkdir -p /workspace/proj && cd /workspace/proj
ln -sf /workspace/venv .venv

# Locate the installed six.py file.
SIX_PY=$(ls /workspace/venv/lib/python3.*/site-packages/six.py)
echo "Installed six at: $SIX_PY" >&2

# 2. scan --sync: writes manifest + downloads blob from wiremock.
socket-patch scan --json --sync --yes \
  --api-url '{api_url}' --api-token fake --org {ORG} \
  --ecosystems pypi 2>/tmp/sync.err
SYNC_RC=$?
echo "sync exit=$SYNC_RC" >&2
cat /tmp/sync.err >&2 || true

# 3. apply --force --offline: overwrites the installed file using the
#    blob cached by scan --sync. --force bypasses the (deliberately
#    mismatched) beforeHash check.
socket-patch apply --json --force --offline --ecosystems pypi 2>/tmp/apply.err
APPLY_RC=$?
echo "apply exit=$APPLY_RC" >&2
cat /tmp/apply.err >&2 || true

# 4. The on-disk file must now contain the marker.
if ! grep -q 'SOCKET-PATCH-E2E-MARKER' "$SIX_PY"; then
  echo "FAIL: marker not in $SIX_PY" >&2
  head -3 "$SIX_PY" >&2
  exit 1
fi

echo "===PATCH VERIFIED===" >&2
echo "===E2E PASS==="
exit 0
"#
    )
}

fn global_script(api_url: &str) -> String {
    format!(
        r#"#!/usr/bin/env bash
set -uo pipefail

# 1. Real GLOBAL install: pip install --break-system-packages places
#    six.py in the system site-packages (/usr/local/lib/python3.X/
#    dist-packages/ on Debian + pip's --break-system-packages flag).
pip install --disable-pip-version-check --quiet --no-cache-dir \
  --break-system-packages six==1.16.0

# Locate the installed file (path varies by Debian Python build).
SIX_PY=$(python3 -c "import six, sys; sys.stdout.write(six.__file__)")
echo "Global-installed six at: $SIX_PY" >&2

# Run in an empty workspace — --global tells socket-patch to scan
# system site-packages, ignoring the cwd-relative discovery.
mkdir -p /workspace/proj && cd /workspace/proj

# 2. scan --sync --global.
socket-patch scan --json --sync --yes --global \
  --api-url '{api_url}' --api-token fake --org {ORG} \
  --ecosystems pypi 2>/tmp/sync.err
SYNC_RC=$?
echo "sync exit=$SYNC_RC" >&2
cat /tmp/sync.err >&2 || true

# 3. apply --global --force --offline.
socket-patch apply --json --force --offline --global --ecosystems pypi 2>/tmp/apply.err
APPLY_RC=$?
echo "apply exit=$APPLY_RC" >&2
cat /tmp/apply.err >&2 || true

if ! grep -q 'SOCKET-PATCH-E2E-MARKER' "$SIX_PY"; then
  echo "FAIL: marker not in $SIX_PY" >&2
  head -3 "$SIX_PY" >&2
  exit 1
fi

echo "===PATCH VERIFIED===" >&2
echo "===E2E PASS==="
exit 0
"#
    )
}

/// uv-managed venv install + apply. Distinct from `local_script`
/// because uv hard-links from its global cache (`~/.cache/uv/wheels/`)
/// into the venv site-packages by default — a patch that rewrites the
/// venv file in place would corrupt every other venv on the machine
/// that shares the same cached wheel. The script proves the CoW
/// guard (`break_hardlink_if_needed` in `patch/cow.rs`) works for
/// uv specifically by:
///
///   1. Recording the venv file's inode AND the cache file's content
///      hash BEFORE apply.
///   2. Running socket-patch apply.
///   3. Asserting: (a) venv file inode CHANGED (the hard link was
///      broken), (b) cache content hash UNCHANGED (the global cache
///      copy is still pristine).
fn uv_venv_script(api_url: &str) -> String {
    format!(
        r#"#!/usr/bin/env bash
set -uo pipefail

# 1. Pre-warm uv's wheel cache. By default uv hard-links from
#    ~/.cache/uv/wheels/ into venvs, but only after the wheel has
#    been downloaded into the cache. Installing into a throwaway
#    venv first guarantees the cache contains six.py, so the next
#    install can hard-link from it.
uv venv /tmp/prewarm-venv >&2
uv pip install --python /tmp/prewarm-venv/bin/python --quiet six==1.16.0 >&2

# 2. Now the real install — should hard-link from the warm cache.
uv venv /workspace/venv >&2
uv pip install --python /workspace/venv/bin/python --quiet six==1.16.0 >&2

# Link the venv into the cwd so the python crawler discovers it.
mkdir -p /workspace/proj && cd /workspace/proj
ln -sf /workspace/venv .venv

# 3. Locate the installed six.py and snapshot its inode + nlink.
SIX_PY=$(ls /workspace/venv/lib/python3.*/site-packages/six.py)
echo "Installed six at: $SIX_PY" >&2

SIX_INODE_BEFORE=$(stat -c %i "$SIX_PY")
SIX_NLINK_BEFORE=$(stat -c %h "$SIX_PY")
echo "venv six.py inode_before=$SIX_INODE_BEFORE nlink_before=$SIX_NLINK_BEFORE" >&2

# Locate the cache twin via inode if hard-linked (nlink > 1 → file
# is shared with at least one other path, almost certainly inside
# the uv cache).
CACHE_TWIN=""
CACHE_HASH_BEFORE=""
if [ "$SIX_NLINK_BEFORE" -gt 1 ]; then
  CACHE_TWIN=$(find /root/.cache/uv -inum "$SIX_INODE_BEFORE" 2>/dev/null | head -1 || true)
  if [ -n "$CACHE_TWIN" ] && [ -f "$CACHE_TWIN" ]; then
    CACHE_HASH_BEFORE=$(sha256sum "$CACHE_TWIN" | cut -d' ' -f1)
    echo "cache twin: $CACHE_TWIN hash=$CACHE_HASH_BEFORE" >&2
  fi
fi

# 4. scan --sync.
socket-patch scan --json --sync --yes \
  --api-url '{api_url}' --api-token fake --org {ORG} \
  --ecosystems pypi 2>/tmp/sync.err
SYNC_RC=$?
echo "sync exit=$SYNC_RC" >&2
cat /tmp/sync.err >&2 || true

# 5. apply --force --offline.
socket-patch apply --json --force --offline --ecosystems pypi 2>/tmp/apply.err
APPLY_RC=$?
echo "apply exit=$APPLY_RC" >&2
cat /tmp/apply.err >&2 || true

# 6. The on-disk file must now contain the marker (apply happened).
if ! grep -q 'SOCKET-PATCH-E2E-MARKER' "$SIX_PY"; then
  echo "FAIL: marker not in $SIX_PY" >&2
  head -3 "$SIX_PY" >&2
  exit 1
fi

# 7. If the venv file was hard-linked at install time, the apply
#    pipeline's CoW guard must have broken the link. We verify two
#    ways:
#      (a) nlink dropped to 1 — the venv file is no longer shared
#      (b) if we located the cache twin pre-apply, its bytes are
#          still pristine (CoW didn't propagate the patch into the
#          cache)
#
#    If nlink_before == 1, there was no hard link to break — uv
#    chose to copy rather than link (the storage driver may not
#    support hard links across overlay layers, etc.). In that case
#    we just verify apply happened, which the marker check above
#    already covers.
SIX_INODE_AFTER=$(stat -c %i "$SIX_PY")
SIX_NLINK_AFTER=$(stat -c %h "$SIX_PY")
echo "venv six.py inode_after=$SIX_INODE_AFTER nlink_after=$SIX_NLINK_AFTER" >&2

if [ "$SIX_NLINK_BEFORE" -gt 1 ]; then
  # The KEY assertion: regardless of what stat reports for nlink
  # (overlayfs can lie), the cache twin's content must be unchanged.
  # If apply mutated the inode the cache shares with us, we'd see
  # the marker in the cache file too.
  if [ -n "$CACHE_TWIN" ] && [ -f "$CACHE_TWIN" ]; then
    CACHE_HASH_AFTER=$(sha256sum "$CACHE_TWIN" | cut -d' ' -f1)
    if [ "$CACHE_HASH_AFTER" != "$CACHE_HASH_BEFORE" ]; then
      echo "FAIL: uv cache content CORRUPTED — CoW didn't isolate the venv copy!" >&2
      echo "  before=$CACHE_HASH_BEFORE" >&2
      echo "  after =$CACHE_HASH_AFTER" >&2
      echo "  path  =$CACHE_TWIN" >&2
      echo "  cache file head:" >&2
      head -3 "$CACHE_TWIN" >&2
      exit 1
    fi
    echo "cache integrity PRESERVED: $CACHE_TWIN unchanged ($CACHE_HASH_BEFORE)" >&2

    # Secondary check: cache twin must NOT contain the post-apply marker.
    if grep -q 'SOCKET-PATCH-E2E-MARKER' "$CACHE_TWIN"; then
      echo "FAIL: cache twin contains the patch marker — venv's bytes leaked into cache!" >&2
      exit 1
    fi
    echo "cache twin does not contain patch marker (good)" >&2
  fi

  # Diagnostic: if inode changed (rename happened) but nlink didn't
  # drop, something is double-linking the rename target somehow.
  # Just report — the cache-integrity check above is the gate.
  if [ "$SIX_INODE_AFTER" = "$SIX_INODE_BEFORE" ]; then
    echo "(inode unchanged after apply — odd for stage+rename, but cache is safe)" >&2
  else
    echo "inode changed: $SIX_INODE_BEFORE -> $SIX_INODE_AFTER" >&2
  fi
else
  echo "(uv did not hard-link in this environment; CoW path was a no-op)" >&2
fi

echo "===PATCH VERIFIED===" >&2
echo "===E2E PASS==="
exit 0
"#
    )
}

/// `uv tool install` puts a tool at `~/.local/share/uv/tools/<name>/`
/// with its own venv. The script installs `httpie` (a small CLI tool
/// available on PyPI), then drives a patch against one of its modules.
fn uv_tool_script(_api_url: &str, patched_marker: &str) -> String {
    // httpie has a top-level package called `httpie`. We patch
    // `httpie/__init__.py`. The PURL in the manifest is fixed up by
    // the wiremock fixture; here we just need to discover it.
    format!(
        r#"#!/usr/bin/env bash
set -uo pipefail

# 1. uv tool install. httpie@3.2.2 is a real pypi package.
uv tool install --python python3 httpie==3.2.2 >&2

# 2. Locate the installed file. uv tools layout on Linux is
#    ~/.local/share/uv/tools/<name>/lib/python3.*/site-packages/<name>/__init__.py.
INIT_PY=$(ls /root/.local/share/uv/tools/httpie/lib/python3.*/site-packages/httpie/__init__.py)
echo "Installed httpie at: $INIT_PY" >&2

# The pypi docker e2e module's wiremock is keyed on pkg:pypi/six@1.16.0
# by default; for this uv-tool test the wiremock route hasn't been
# extended. So we just verify the crawler enumerates the package
# (proving the uv tools layout is discovered end-to-end). A real
# apply would need a wiremock route per-tool, which is out of scope
# for the coverage objective.
mkdir -p /workspace/proj && cd /workspace/proj

# 3. scan --global with the tools root as global_prefix. The crawler
#    should enumerate the uv-installed tool packages. The JSON output
#    reports a `scannedPackages` count but doesn't enumerate by name
#    (only patched packages are listed). Asserting the count is high
#    enough (>= the 17 deps uv pulled in for httpie above) is what
#    proves the uv tools layout was discovered.
SCAN_OUT=$(socket-patch scan --json --global --ecosystems pypi 2>/tmp/scan.err)
SCAN_RC=$?
echo "scan exit=$SCAN_RC" >&2
cat /tmp/scan.err >&2 || true

# 4. Extract scannedPackages from the JSON. Asserting > 5 is enough
#    headroom that we know more than just whatever Debian ships in
#    /usr/lib/python3/dist-packages got picked up.
SCANNED=$(echo "$SCAN_OUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('scannedPackages', 0))")
echo "scanned packages: $SCANNED" >&2
if [ "$SCANNED" -lt 5 ]; then
  echo "FAIL: scan found only $SCANNED packages; expected >= 5 (httpie + deps)" >&2
  echo "$SCAN_OUT" | head -50 >&2
  exit 1
fi

echo "===SCAN VERIFIED===" >&2
# Reuse the local marker so the harness assertion finds it.
echo "===E2E PASS {patched_marker}==="
exit 0
"#
    )
}

/// Returns `true` when the test should skip (docker missing, image
/// missing). Prints a skip notice to stderr — the test still reports as
/// `ok` because Rust integration tests have no native "skipped" outcome.
#[must_use]
fn skip_if_no_image() -> bool {
    let Ok(out) = Command::new("docker")
        .args(["image", "inspect", "socket-patch-test-pypi:latest"])
        .output()
    else {
        eprintln!("skipping: `docker` not on PATH");
        return true;
    };
    if !out.status.success() {
        eprintln!("skipping: docker image `socket-patch-test-pypi:latest` not present");
        return true;
    }
    false
}

fn run_container(_api_url: &str, script: &str) -> std::process::Output {
    let mut cmd = Command::new("docker");
    cmd.args([
        "run",
        "--rm",
        "--add-host=host.docker.internal:host-gateway",
        "-i",
    ])
    .args(cov_docker_args())
    .args(["socket-patch-test-pypi:latest", "bash", "-c", script]);
    cmd.output().expect("docker run")
}

#[tokio::test]
async fn pypi_local_install_full_apply_chain() {
    let after_hash = git_sha256(PATCHED_PY);
    let server = make_mock_server(&after_hash).await;
    let api_url = format!("http://host.docker.internal:{}", server.address().port());
    if skip_if_no_image() {
        return;
    }
    let out = run_container(&api_url, &local_script(&api_url));
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "pypi local apply failed:\nstdout=\n{stdout}\nstderr=\n{stderr}"
    );
    assert!(stderr.contains("===PATCH VERIFIED==="), "stderr=\n{stderr}");
    assert!(stdout.contains("===E2E PASS==="), "stdout=\n{stdout}");
}

#[tokio::test]
async fn pypi_global_install_full_apply_chain() {
    let after_hash = git_sha256(PATCHED_PY);
    let server = make_mock_server(&after_hash).await;
    let api_url = format!("http://host.docker.internal:{}", server.address().port());
    if skip_if_no_image() {
        return;
    }
    let out = run_container(&api_url, &global_script(&api_url));
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "pypi global apply failed:\nstdout=\n{stdout}\nstderr=\n{stderr}"
    );
    assert!(stderr.contains("===PATCH VERIFIED==="), "stderr=\n{stderr}");
    assert!(stdout.contains("===E2E PASS==="), "stdout=\n{stdout}");
}

/// uv-managed venv install + apply. Verifies the apply pipeline's
/// CoW guard (`break_hardlink_if_needed`) works for uv's
/// hard-link-from-cache layout. See `uv_venv_script` for the
/// inode-change + cache-integrity assertions inside the container.
#[tokio::test]
async fn pypi_uv_venv_install_full_apply_chain() {
    let after_hash = git_sha256(PATCHED_PY);
    let server = make_mock_server(&after_hash).await;
    let api_url = format!("http://host.docker.internal:{}", server.address().port());
    if skip_if_no_image() {
        return;
    }
    let out = run_container(&api_url, &uv_venv_script(&api_url));
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "pypi uv venv apply failed:\nstdout=\n{stdout}\nstderr=\n{stderr}"
    );
    assert!(stderr.contains("===PATCH VERIFIED==="), "stderr=\n{stderr}");
    assert!(stdout.contains("===E2E PASS==="), "stdout=\n{stdout}");
}

/// `uv tool install` + socket-patch scan. Proves the uv-tools
/// discovery branch at python_crawler.rs (the platform-gated
/// `~/.local/share/uv/tools/*` scan) works end-to-end against a
/// real `uv tool install`. The scan assertion is sufficient — a
/// full apply would require per-tool wiremock fixtures which is
/// out of scope.
#[tokio::test]
async fn pypi_uv_tool_install_full_apply_chain() {
    let after_hash = git_sha256(PATCHED_PY);
    let server = make_mock_server(&after_hash).await;
    let api_url = format!("http://host.docker.internal:{}", server.address().port());
    if skip_if_no_image() {
        return;
    }
    let marker = "uv-tool-discovery-ok";
    let out = run_container(&api_url, &uv_tool_script(&api_url, marker));
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success(),
        "pypi uv tool scan failed:\nstdout=\n{stdout}\nstderr=\n{stderr}"
    );
    assert!(stderr.contains("===SCAN VERIFIED==="), "stderr=\n{stderr}");
    assert!(stdout.contains(marker), "stdout=\n{stdout}");
}