askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_unit_prices.py · 7.4 KB · 182 lines
| 1 | """Media pricing: a lookup, not a rate, and it must exist before the call. |
| 2 | |
| 3 | `ai_prices` prices a token and object_ai.compute_cost_cents multiplies. |
| 4 | Media cannot be expressed in that shape at all -- an image has a PRICE |
| 5 | per (model, quality, size), and video has a price per second per (model, |
| 6 | size). Pretending otherwise is how a $6.00 sora-2-pro run gets quoted at |
| 7 | zero and billed at six dollars after the fact. |
| 8 | |
| 9 | The refusals in this file matter more than the arithmetic. A media run is |
| 10 | held against the wallet at SUBMISSION, so a price that cannot be computed |
| 11 | in advance is a run that cannot be held -- and the only safe answer is to |
| 12 | refuse it up front rather than discover the number once the provider has |
| 13 | already been paid. |
| 14 | """ |
| 15 | |
| 16 | import csv |
| 17 | import pathlib |
| 18 | |
| 19 | import object_unit_prices as unit_prices |
| 20 | |
| 21 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 22 | SEED = REPO_ROOT / "packages" / "app-settings" / "seed" / "unit_prices.tsv" |
| 23 | |
| 24 | |
| 25 | def seeded_rows(): |
| 26 | with SEED.open() as handle: |
| 27 | return list(csv.DictReader(handle, delimiter="\t")) |
| 28 | |
| 29 | |
| 30 | def row(**fields): |
| 31 | base = {"id": "r1", "kind": "image", "model": "m", "quality": "", |
| 32 | "size": "", "unit_price_cents": "10"} |
| 33 | base.update(fields) |
| 34 | return base |
| 35 | |
| 36 | |
| 37 | # --- images: a flat price per item ---------------------------------------------- |
| 38 | |
| 39 | def test_an_image_is_priced_by_model_quality_and_size(): |
| 40 | result = unit_prices.quote(seeded_rows(), { |
| 41 | "kind": "image", "model": "gpt-image-2", "quality": "high", |
| 42 | "size": "1536x1024"}) |
| 43 | assert result["price_cents"] == 30 |
| 44 | assert result["price_row_id"] == "gpt-image-2-high-1536x1024" |
| 45 | |
| 46 | |
| 47 | def test_the_same_model_at_a_different_size_is_a_different_price(): |
| 48 | """The reason this is a table and not a formula: nothing derives one |
| 49 | from the other.""" |
| 50 | cheap = unit_prices.quote(seeded_rows(), { |
| 51 | "kind": "image", "model": "gpt-image-2", "quality": "low", |
| 52 | "size": "1024x1024"})["price_cents"] |
| 53 | dear = unit_prices.quote(seeded_rows(), { |
| 54 | "kind": "image", "model": "gpt-image-2", "quality": "high", |
| 55 | "size": "1536x1024"})["price_cents"] |
| 56 | assert (cheap, dear) == (2, 30) |
| 57 | |
| 58 | |
| 59 | def test_a_batch_multiplies_by_quantity(): |
| 60 | result = unit_prices.quote(seeded_rows(), { |
| 61 | "kind": "image", "model": "gpt-image-2", "quality": "low", |
| 62 | "size": "1024x1024", "quantity": "4"}) |
| 63 | assert result["price_cents"] == 8 |
| 64 | |
| 65 | |
| 66 | # --- video: per second, and the number that makes holds necessary --------------- |
| 67 | |
| 68 | def test_a_twelve_second_sora_pro_run_costs_six_dollars(): |
| 69 | """The number from the spec, and the reason media needs a hold at |
| 70 | submission rather than a charge at completion.""" |
| 71 | result = unit_prices.quote(seeded_rows(), { |
| 72 | "kind": "video", "model": "sora-2-pro", "size": "1792x1024", |
| 73 | "duration_seconds": "12"}) |
| 74 | assert result["price_cents"] == 600 |
| 75 | assert "50c/s" in result["detail"] |
| 76 | |
| 77 | |
| 78 | def test_video_without_a_duration_cannot_be_quoted_in_advance(): |
| 79 | """Not a default of one second. A run whose price is unknowable |
| 80 | before the call is a run that cannot be held, which is exactly the |
| 81 | case that must be refused.""" |
| 82 | result = unit_prices.quote(seeded_rows(), { |
| 83 | "kind": "video", "model": "sora-2", "size": "1280x720"}) |
| 84 | assert "duration_seconds is required" in result["error"] |
| 85 | assert "price_cents" not in result |
| 86 | |
| 87 | |
| 88 | def test_a_fractional_duration_rounds_half_up_once_at_the_end(): |
| 89 | """Per-second times a fraction must not lose money to truncation, and |
| 90 | must not round each step.""" |
| 91 | rows = [row(kind="video", model="v", size="", unit_price_cents="10", |
| 92 | quality="")] |
| 93 | assert unit_prices.quote(rows, { |
| 94 | "kind": "video", "model": "v", "duration_seconds": "2.55", |
| 95 | })["price_cents"] == 26 # 25.5 -> 26, not 25 |
| 96 | |
| 97 | |
| 98 | # --- the refusals --------------------------------------------------------------- |
| 99 | |
| 100 | def test_an_unpriced_model_is_refused_and_says_what_to_configure(): |
| 101 | result = unit_prices.quote(seeded_rows(), { |
| 102 | "kind": "image", "model": "midjourney-9", "quality": "low", |
| 103 | "size": "1024x1024"}) |
| 104 | assert "No unit price for image" in result["error"] |
| 105 | assert "midjourney-9" in result["error"] |
| 106 | assert "unit_prices" in result["error"] |
| 107 | |
| 108 | |
| 109 | def test_a_near_miss_never_falls_back_to_a_cheaper_neighbour(): |
| 110 | """THE dangerous convenience. gpt-image-2 at an unlisted size must |
| 111 | not quietly quote the 1024x1024 price -- that is a number the |
| 112 | provider will not honour, discovered only on the invoice.""" |
| 113 | result = unit_prices.quote(seeded_rows(), { |
| 114 | "kind": "image", "model": "gpt-image-2", "quality": "low", |
| 115 | "size": "4096x4096"}) |
| 116 | assert "error" in result |
| 117 | |
| 118 | |
| 119 | def test_an_unknown_media_kind_is_refused_naming_the_known_ones(): |
| 120 | result = unit_prices.quote(seeded_rows(), {"kind": "hologram"}) |
| 121 | assert "Known kinds" in result["error"] |
| 122 | |
| 123 | |
| 124 | def test_a_row_with_no_usable_price_is_refused_rather_than_treated_as_free(): |
| 125 | rows = [row(unit_price_cents="")] |
| 126 | result = unit_prices.quote(rows, {"kind": "image", "model": "m", |
| 127 | "quality": "", "size": ""}) |
| 128 | assert "no usable unit_price_cents" in result["error"] |
| 129 | |
| 130 | |
| 131 | def test_zero_is_a_real_price_and_not_a_missing_one(): |
| 132 | """A genuinely free model must quote 0, not refuse -- the free-run |
| 133 | path through the runner depends on the distinction.""" |
| 134 | rows = [row(unit_price_cents="0")] |
| 135 | result = unit_prices.quote(rows, {"kind": "image", "model": "m", |
| 136 | "quality": "", "size": ""}) |
| 137 | assert result["price_cents"] == 0 |
| 138 | |
| 139 | |
| 140 | # --- wildcards ------------------------------------------------------------------ |
| 141 | |
| 142 | def test_a_blank_dimension_is_a_wildcard(): |
| 143 | """A provider charging one price for every size should not have to |
| 144 | enumerate sizes.""" |
| 145 | rows = [row(id="any-size", model="m", quality="low", size="")] |
| 146 | result = unit_prices.quote(rows, { |
| 147 | "kind": "image", "model": "m", "quality": "low", "size": "9999x1"}) |
| 148 | assert result["price_row_id"] == "any-size" |
| 149 | |
| 150 | |
| 151 | def test_an_exact_row_beats_a_wildcard(): |
| 152 | """So a specific price can be added later without deleting the |
| 153 | catch-all, which is what makes the table safe to edit.""" |
| 154 | rows = [row(id="any-size", model="m", quality="low", size="", |
| 155 | unit_price_cents="10"), |
| 156 | row(id="exact", model="m", quality="low", size="1024x1024", |
| 157 | unit_price_cents="99")] |
| 158 | result = unit_prices.quote(rows, { |
| 159 | "kind": "image", "model": "m", "quality": "low", "size": "1024x1024"}) |
| 160 | assert result["price_row_id"] == "exact" |
| 161 | assert result["price_cents"] == 99 |
| 162 | |
| 163 | |
| 164 | def test_the_wildcard_still_does_not_match_a_different_model(): |
| 165 | """Wildcards widen a dimension, never the identity of the thing.""" |
| 166 | rows = [row(id="any-size", model="m", quality="low", size="")] |
| 167 | assert "error" in unit_prices.quote(rows, { |
| 168 | "kind": "image", "model": "other", "quality": "low", "size": "x"}) |
| 169 | |
| 170 | |
| 171 | # --- the seeded table itself ---------------------------------------------------- |
| 172 | |
| 173 | def test_every_seeded_row_prices_without_error(): |
| 174 | """A seed that cannot be quoted is a seed that will refuse every run |
| 175 | it was added to enable.""" |
| 176 | for seeded in seeded_rows(): |
| 177 | spec = {"kind": seeded["kind"], "model": seeded["model"], |
| 178 | "quality": seeded["quality"], "size": seeded["size"]} |
| 179 | if seeded["kind"] in unit_prices.PER_SECOND_KINDS: |
| 180 | spec["duration_seconds"] = "1" |
| 181 | result = unit_prices.quote(seeded_rows(), spec) |
| 182 | assert "price_cents" in result, (seeded["id"], result) |