|
| 1 | +# Copyright 2026 Apple Inc. |
| 2 | +# |
| 3 | +# Use of this source code is governed by a BSD-3-clause license that can |
| 4 | +# be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause |
| 5 | + |
| 6 | +"""Tests for the ``coreai.segmentation.export`` CLI. |
| 7 | +
|
| 8 | +Flag plumbing only — nothing here downloads weights or runs an export, so |
| 9 | +these are safe on any machine. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import argparse |
| 15 | +import logging |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from coreai_models.segmentation.export import ( |
| 20 | + _SUPPORTED, |
| 21 | + _resolve_hf_model_id, |
| 22 | + _resolve_image_size, |
| 23 | + _warn_unused_flags, |
| 24 | + build_parser, |
| 25 | +) |
| 26 | +from coreai_models.segmentation.pipeline import FullExportConfig, SegmentationExportConfig |
| 27 | + |
| 28 | + |
| 29 | +def _parse(*argv: str) -> tuple[argparse.ArgumentParser, argparse.Namespace]: |
| 30 | + """Build a fresh parser and parse ``argv``, returning both.""" |
| 31 | + parser = build_parser() |
| 32 | + return parser, parser.parse_args(list(argv)) |
| 33 | + |
| 34 | + |
| 35 | +# --- --model --------------------------------------------------------- |
| 36 | + |
| 37 | + |
| 38 | +def test_every_supported_spelling_parses_and_resolves() -> None: |
| 39 | + """``--model`` takes its ``choices`` from ``_SUPPORTED``, which is what lets |
| 40 | + ``_resolve_hf_model_id`` be a bare dict lookup. Pin that coupling: every |
| 41 | + key must parse, and every value must be a canonical HF id.""" |
| 42 | + for spelling in _SUPPORTED: |
| 43 | + _, args = _parse("--model", spelling) |
| 44 | + assert _resolve_hf_model_id(args.model) == _SUPPORTED[spelling] |
| 45 | + |
| 46 | + |
| 47 | +def test_model_defaults_to_sam3() -> None: |
| 48 | + _, args = _parse() |
| 49 | + assert _resolve_hf_model_id(args.model) == "facebook/sam3" |
| 50 | + |
| 51 | + |
| 52 | +def test_model_accepts_registry_short_name() -> None: |
| 53 | + _, args = _parse("--model", "sam3") |
| 54 | + assert _resolve_hf_model_id(args.model) == "facebook/sam3" |
| 55 | + |
| 56 | + |
| 57 | +def test_model_accepts_hf_id() -> None: |
| 58 | + _, args = _parse("--model", "facebook/sam3") |
| 59 | + assert _resolve_hf_model_id(args.model) == "facebook/sam3" |
| 60 | + |
| 61 | + |
| 62 | +def test_model_rejects_unknown_value() -> None: |
| 63 | + parser = build_parser() |
| 64 | + with pytest.raises(SystemExit): |
| 65 | + parser.parse_args(["--model", "facebook/sam2"]) |
| 66 | + |
| 67 | + |
| 68 | +def test_bare_positional_is_rejected() -> None: |
| 69 | + """``--model`` replaced a positional, so a bare value is no longer valid.""" |
| 70 | + parser = build_parser() |
| 71 | + with pytest.raises(SystemExit): |
| 72 | + parser.parse_args(["sam3"]) |
| 73 | + |
| 74 | + |
| 75 | +# --- mode + image size ----------------------------------------------- |
| 76 | + |
| 77 | + |
| 78 | +def test_lite_is_the_default_mode() -> None: |
| 79 | + _, args = _parse() |
| 80 | + assert args.full is False |
| 81 | + |
| 82 | + |
| 83 | +def test_image_size_defaults_per_mode() -> None: |
| 84 | + _, lite = _parse() |
| 85 | + _, full = _parse("--full") |
| 86 | + assert _resolve_image_size(lite) == SegmentationExportConfig.image_size |
| 87 | + assert _resolve_image_size(full) == FullExportConfig.image_size |
| 88 | + |
| 89 | + |
| 90 | +def test_explicit_image_size_overrides_mode_default() -> None: |
| 91 | + _, args = _parse("--full", "--image-size", "512") |
| 92 | + assert _resolve_image_size(args) == 512 |
| 93 | + |
| 94 | + |
| 95 | +# --- _warn_unused_flags ---------------------------------------------- |
| 96 | + |
| 97 | + |
| 98 | +def test_warn_unused_flags_does_not_reparse_argv() -> None: |
| 99 | + """Regression: this recovered defaults via ``parse_args([args.model])``, |
| 100 | + which argparse rejected as a stray positional once ``model`` became |
| 101 | + ``--model`` — killing the process before the export started.""" |
| 102 | + _, args = _parse("--full", "--dtype", "float16") |
| 103 | + _warn_unused_flags(args) |
| 104 | + |
| 105 | + |
| 106 | +def test_warns_on_dtype_equal_to_default_in_lite_mode( |
| 107 | + caplog: pytest.LogCaptureFixture, |
| 108 | +) -> None: |
| 109 | + """Regression: comparing against the default missed ``--dtype float32``, |
| 110 | + since float32 *is* the default — the flag was silently ignored with no |
| 111 | + warning. Mode-specific flags now default to None so "passed" is detectable |
| 112 | + regardless of the value.""" |
| 113 | + _, args = _parse("--dtype", "float32") |
| 114 | + with caplog.at_level(logging.WARNING): |
| 115 | + _warn_unused_flags(args) |
| 116 | + assert "--dtype" in caplog.text |
| 117 | + |
| 118 | + |
| 119 | +def test_warns_on_lite_only_flag_equal_to_default_in_full_mode( |
| 120 | + caplog: pytest.LogCaptureFixture, |
| 121 | +) -> None: |
| 122 | + """Same class of bug as the --dtype case: 32 is the resolved default.""" |
| 123 | + _, args = _parse("--full", "--max-text-seq-len", "32") |
| 124 | + with caplog.at_level(logging.WARNING): |
| 125 | + _warn_unused_flags(args) |
| 126 | + assert "--max-text-seq-len" in caplog.text |
| 127 | + |
| 128 | + |
| 129 | +def test_warns_on_lite_only_flags_in_full_mode(caplog: pytest.LogCaptureFixture) -> None: |
| 130 | + _, args = _parse("--full", "--n-bits", "4", "--max-text-seq-len", "64") |
| 131 | + with caplog.at_level(logging.WARNING): |
| 132 | + _warn_unused_flags(args) |
| 133 | + assert "--n-bits" in caplog.text |
| 134 | + assert "--max-text-seq-len" in caplog.text |
| 135 | + |
| 136 | + |
| 137 | +def test_warns_on_dtype_in_lite_mode(caplog: pytest.LogCaptureFixture) -> None: |
| 138 | + _, args = _parse("--dtype", "float16") |
| 139 | + with caplog.at_level(logging.WARNING): |
| 140 | + _warn_unused_flags(args) |
| 141 | + assert "--dtype" in caplog.text |
| 142 | + |
| 143 | + |
| 144 | +def test_no_warning_for_lite_flags_in_lite_mode(caplog: pytest.LogCaptureFixture) -> None: |
| 145 | + _, args = _parse("--n-bits", "4", "--group-size", "16") |
| 146 | + with caplog.at_level(logging.WARNING): |
| 147 | + _warn_unused_flags(args) |
| 148 | + assert caplog.text == "" |
| 149 | + |
| 150 | + |
| 151 | +def test_no_warning_for_dtype_in_full_mode(caplog: pytest.LogCaptureFixture) -> None: |
| 152 | + _, args = _parse("--full", "--dtype", "float16") |
| 153 | + with caplog.at_level(logging.WARNING): |
| 154 | + _warn_unused_flags(args) |
| 155 | + assert caplog.text == "" |
| 156 | + |
| 157 | + |
| 158 | +def test_no_warning_when_nothing_mode_specific_is_passed( |
| 159 | + caplog: pytest.LogCaptureFixture, |
| 160 | +) -> None: |
| 161 | + for argv in ((), ("--full",)): |
| 162 | + _, args = _parse(*argv) |
| 163 | + with caplog.at_level(logging.WARNING): |
| 164 | + _warn_unused_flags(args) |
| 165 | + assert caplog.text == "", f"unexpected warning for {argv}" |
0 commit comments