test_cost_raise.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """scripts/cost_raise.py: a dry-run-first helper for raising one phase's ceiling."""
  2. import json
  3. import shutil
  4. import sys
  5. from pathlib import Path
  6. sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
  7. import cost_check
  8. import cost_raise
  9. ROOT = Path(__file__).resolve().parents[1]
  10. # Tests that need a known lifecycle state (stage-1 current, stage-2 planned) use a frozen
  11. # copy of approved.json, so advancing the real file's current phase does not break them.
  12. FIXTURE_STAGE1 = ROOT / "tests/fixtures/approved_stage1_current.json"
  13. def repo(tmp_path):
  14. for rel in ("docs/stages/approved.json", "docs/cost-prices.json", "docs/cost-prices"):
  15. src, dst = ROOT / rel, tmp_path / rel
  16. dst.parent.mkdir(parents=True, exist_ok=True)
  17. if src.is_dir():
  18. shutil.copytree(src, dst)
  19. else:
  20. shutil.copy(src, dst)
  21. shutil.copy(FIXTURE_STAGE1, tmp_path / "docs/stages/approved.json")
  22. for rel in ("stacks", "modules", ".spacelift"):
  23. if (ROOT / rel).exists():
  24. shutil.copytree(
  25. ROOT / rel, tmp_path / rel, ignore=shutil.ignore_patterns(".terraform")
  26. )
  27. return tmp_path
  28. def raise_cmd(root, *extra):
  29. extra = extra if "--no-cascade" in extra else ("--cascade", *extra)
  30. extra = tuple(e for e in extra if e != "--no-cascade")
  31. return [
  32. "--root", str(root), "--phase", "stage-3", "--ceiling", "36",
  33. "--reason", "bigger droplet", "--decided", "2026-10-17", *extra,
  34. ] # fmt: skip
  35. def read(root):
  36. return json.loads((root / "docs/stages/approved.json").read_text())
  37. def test_the_real_file_round_trips_through_render():
  38. text = (ROOT / "docs/stages/approved.json").read_text()
  39. assert cost_raise.render(json.loads(text)) == text
  40. def test_a_raise_that_leaves_a_later_phase_lower_needs_cascade(tmp_path, capsys):
  41. root = repo(tmp_path)
  42. assert cost_raise.main(raise_cmd(root, "--no-cascade", "--write")) == 1
  43. assert "stage-4" in capsys.readouterr().err
  44. assert read(root)["phases"]["stage-3"]["ceiling"] == 30.0
  45. def test_cascade_raises_later_phases_and_notes_why(tmp_path):
  46. root = repo(tmp_path)
  47. assert cost_raise.main(raise_cmd(root, "--write")) == 0
  48. phases = read(root)["phases"]
  49. assert phases["stage-4"]["ceiling"] == 36.0
  50. assert "together with stage-3" in phases["stage-4"]["reason"]
  51. assert phases["stage-4"]["decided"] == "2026-10-17"
  52. assert phases["stage-2"]["ceiling"] == 30.0
  53. assert phases["edge"]["ceiling"] == 36.0 # edge was 35, now raised with stage-3
  54. def test_dry_run_prints_a_diff_and_writes_nothing(tmp_path, capsys):
  55. root = repo(tmp_path)
  56. before = (root / "docs/stages/approved.json").read_text()
  57. assert cost_raise.main(raise_cmd(root)) == 0
  58. out = capsys.readouterr().out
  59. assert '- "ceiling": 30.0,' in out and '+ "ceiling": 36.0,' in out
  60. assert "bigger droplet" in out and "2026-10-17" in out
  61. assert "dry run" in out
  62. assert (root / "docs/stages/approved.json").read_text() == before
  63. def test_write_edits_the_file_and_the_gate_still_passes(tmp_path, capsys):
  64. root = repo(tmp_path)
  65. assert cost_raise.main(raise_cmd(root, "--write")) == 0
  66. phase = read(root)["phases"]["stage-3"]
  67. assert (phase["ceiling"], phase["reason"], phase["decided"]) == (
  68. 36.0, "bigger droplet", "2026-10-17",
  69. ) # fmt: skip
  70. assert phase["status"] == "planned"
  71. capsys.readouterr()
  72. assert cost_check.main(["--root", str(root)]) == 0
  73. def test_a_provider_approval_is_never_touched(tmp_path):
  74. root = repo(tmp_path)
  75. before = read(root)["providers"]
  76. cost_raise.main(raise_cmd(root, "--write"))
  77. assert read(root)["providers"] == before
  78. def test_make_current_moves_the_current_phase(tmp_path):
  79. root = repo(tmp_path)
  80. args = raise_cmd(root, "--make-current", "--write")
  81. args[args.index("stage-3")] = "stage-2"
  82. assert cost_raise.main(args) == 0
  83. doc = read(root)
  84. assert doc["current_phase"] == "stage-2"
  85. assert doc["phases"]["stage-2"]["status"] == "current"
  86. assert doc["phases"]["stage-1"]["status"] == "done"
  87. assert cost_check.phase_problems(doc) == []
  88. def test_a_lower_ceiling_is_refused_without_lowers(tmp_path, capsys):
  89. root = repo(tmp_path)
  90. args = raise_cmd(root, "--write")
  91. args[args.index("36")] = "20"
  92. before = (root / "docs/stages/approved.json").read_text()
  93. assert cost_raise.main(args) == 1
  94. assert "lowers" in capsys.readouterr().err
  95. assert (root / "docs/stages/approved.json").read_text() == before
  96. def test_a_lower_ceiling_passes_with_lowers(tmp_path):
  97. root = repo(tmp_path)
  98. args = raise_cmd(root, "--lowers", "--write")
  99. args[args.index("36")] = "20"
  100. assert cost_raise.main(args) == 0
  101. assert read(root)["phases"]["stage-3"]["lowers"] is True
  102. def test_a_new_phase_is_added_after_the_named_one(tmp_path):
  103. root = repo(tmp_path)
  104. args = [
  105. "--root", str(root), "--phase", "mid", "--ceiling", "30", "--reason", "r",
  106. "--decided", "2026-10-17", "--after", "stage-1", "--write",
  107. ] # fmt: skip
  108. assert cost_raise.main(args) == 0
  109. keys = list(read(root)["phases"])
  110. assert keys.index("mid") == keys.index("stage-1") + 1
  111. def test_a_new_phase_with_no_after_is_appended(tmp_path):
  112. root = repo(tmp_path)
  113. args = [
  114. "--root", str(root), "--phase", "zeta", "--ceiling", "59", "--reason", "r",
  115. "--decided", "2026-10-17", "--write",
  116. ] # fmt: skip
  117. assert cost_raise.main(args) == 0
  118. assert list(read(root)["phases"])[-1] == "zeta"
  119. def test_raising_past_hard_max_is_refused(tmp_path, capsys):
  120. root = repo(tmp_path)
  121. path = root / "docs/stages/approved.json"
  122. doc = read(root)
  123. doc["hard_max"] = 60.0
  124. path.write_text(cost_raise.render(doc))
  125. args = raise_cmd(root, "--write")
  126. args[args.index("36")] = "70"
  127. assert cost_raise.main(args) == 1
  128. assert "hard_max" in capsys.readouterr().err
  129. assert "70" not in path.read_text()
  130. def test_a_bad_date_or_empty_reason_is_refused(tmp_path, capsys):
  131. root = repo(tmp_path)
  132. args = raise_cmd(root)
  133. args[args.index("2026-10-17")] = "yesterday"
  134. assert cost_raise.main(args) == 1
  135. args = raise_cmd(root)
  136. args[args.index("bigger droplet")] = " "
  137. assert cost_raise.main(args) == 1
  138. capsys.readouterr()
  139. def test_a_nan_ceiling_is_refused(tmp_path):
  140. root = repo(tmp_path)
  141. args = raise_cmd(root)
  142. args[args.index("36")] = "nan"
  143. assert cost_raise.main(args) == 2
  144. def test_a_legacy_file_has_to_be_migrated_first(tmp_path, capsys):
  145. root = repo(tmp_path)
  146. (root / "docs/stages/approved.json").write_text(
  147. json.dumps({"ceiling": 30.0, "providers": read(root)["providers"]})
  148. )
  149. assert cost_raise.main(raise_cmd(root)) == 1
  150. assert "migrate" in capsys.readouterr().err
  151. def test_the_default_date_is_today(tmp_path, capsys):
  152. root = repo(tmp_path)
  153. args = raise_cmd(root)
  154. del args[args.index("--decided") : args.index("--decided") + 2]
  155. assert cost_raise.main(args) == 0
  156. assert "decided" in capsys.readouterr().out