Spaces:
Sleeping
Sleeping
File size: 7,128 Bytes
55626ac | 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 | """Codex image snapshots must survive queues and reject account changes."""
from pathlib import Path
from unittest.mock import patch
from app.models.schema import VideoParams
from app.services import codex_image, codex_oauth, task, webui_task
def context(generation=1, model="test-responses"):
return codex_image.CodexImageContext(model, 300, generation)
def test_queue_snapshots_context_and_deduplicates_by_account():
params = VideoParams(video_subject="Pyramids", video_source="codex_image")
with (
patch.dict(webui_task._active_submissions, {}, clear=True),
patch.object(webui_task, "_enqueue_generation") as enqueue,
patch.object(codex_image, "capture_context", return_value=context()) as capture,
):
assert webui_task.submit_generation("first", params) == "first"
assert webui_task.submit_generation("duplicate", params) == "first"
assert enqueue.call_count == 1
assert enqueue.call_args.args[-1] == context()
capture.return_value = context(2)
assert webui_task.submit_generation("new-account", params) == "new-account"
capture.return_value = context(2, "other-model")
assert webui_task.submit_generation("new-model", params) == "new-model"
assert enqueue.call_count == 3
def test_queue_dedup_distinguishes_persistent_sessions_with_same_generation():
from dataclasses import replace
params = VideoParams(video_subject="Pyramids", video_source="codex_image")
first = replace(
context(0), session=codex_oauth.CodexSessionBinding("account-a", "login-a")
)
other_account = replace(
first, session=codex_oauth.CodexSessionBinding("account-b", "login-b")
)
same_account_reconnected = replace(
first, session=codex_oauth.CodexSessionBinding("account-a", "new-login-a")
)
with (
patch.dict(webui_task._active_submissions, {}, clear=True),
patch.object(webui_task, "_enqueue_generation") as enqueue,
patch.object(codex_image, "capture_context", return_value=first) as capture,
):
assert webui_task.submit_generation("first", params) == "first"
assert webui_task.submit_generation("duplicate", params) == "first"
capture.return_value = other_account
assert webui_task.submit_generation("different-account", params) == "different-account"
capture.return_value = same_account_reconnected
assert webui_task.submit_generation("reconnected", params) == "reconnected"
assert enqueue.call_count == 3
def test_queued_account_change_fails_before_script_or_voice():
params = VideoParams(
video_subject="Pyramids", video_source="codex_image", subtitle_enabled=False
)
with (
patch.object(codex_oauth, "authorization_generation", return_value=2),
patch.object(task, "generate_script") as script,
patch.object(task, "_mark_task_failed", return_value={"failed": True}) as fail,
):
result = task.start("stale", params, codex_image_context=context())
assert result == {"failed": True}
assert fail.call_args.args[1] == "preflight"
script.assert_not_called()
def test_api_captures_context_before_any_consuming_service():
params = VideoParams(
video_subject="Pyramids", video_source="codex_image", subtitle_enabled=False
)
with (
patch.object(
codex_image,
"capture_context",
side_effect=codex_image.CodexImageError("codex.image.model_required"),
),
patch.object(task, "generate_script") as script,
patch.object(task, "_mark_task_failed", return_value=None) as fail,
):
task.start("missing-model", params)
assert fail.call_args.args[1] == "preflight"
assert fail.call_args.kwargs["details"]["error_key"] == "codex.image.model_required"
script.assert_not_called()
def test_worker_forwards_frozen_context():
from contextlib import nullcontext
snapshot = context()
with (
patch.object(
webui_task.config, "runtime_config_lock", return_value=nullcontext()
),
patch.object(task, "start", return_value={}) as start,
):
webui_task._run_generation(
"frozen",
VideoParams(video_subject="Pyramids", video_source="codex_image"),
False,
codex_image_context=snapshot,
)
assert start.call_args.kwargs["codex_image_context"] is snapshot
def test_material_phase_forwards_context_and_retains_translatable_error():
from app.services import material
snapshot = context()
params = VideoParams(video_subject="Pyramids", video_source="codex_image")
error = material.CodexImageMaterialError(
codex_image.CodexImageError("codex.image.invalid_result")
)
with (
patch.object(material, "download_videos", side_effect=error) as download,
patch.object(task, "_mark_task_failed") as fail,
):
assert (
task.get_video_materials(
"image-error", params, ["pyramid"], 3, codex_image_context=snapshot
)
is None
)
assert download.call_args.kwargs["codex_image_context"] is snapshot
assert fail.call_args.kwargs["details"] == {
"error_key": "codex.image.invalid_result",
"error_params": {},
}
def test_cli_and_bootstrap_accept_codex_without_an_api_key(tmp_path, monkeypatch):
import importlib.util
import cli
args = cli.parse_args(["--video-subject", "Pyramids", "--video-source", "codex_image", "--image-motion", "static"])
assert cli.build_video_params(args).video_source == "codex_image"
spec = importlib.util.spec_from_file_location("image_bootstrap", Path(__file__).resolve().parents[2] / "docs/skill/mpt_agent.py")
helper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helper)
config_file = tmp_path / "config.toml"
config_file.write_text('llm_provider="ollama"\ncodex_image_model_name="responses-model"\n', encoding="utf-8")
monkeypatch.setenv("MPT_HOME", str(tmp_path))
provider, missing = helper.missing_config(config_file, ["--video-source", "codex_image"])
assert missing == ["codex_sign_in"]
auth = tmp_path / "storage/auth/openai_codex.bin"
auth.parent.mkdir(parents=True)
auth.write_bytes(b"presence only; helper never decrypts")
assert helper.missing_config(config_file, ["--video-source", "codex_image"])[1] == []
def test_translatable_failure_is_persisted_and_returned_consistently(tmp_path):
from app.services.state import MemoryState
state = MemoryState()
with patch.object(task.sm, "state", state):
state.update_task("translated", state=task.const.TASK_STATE_PROCESSING, error_key=None, error_params=None)
result = task._mark_task_failed("translated", "materials", "sanitized", details={"error_key": "codex.oauth.http_error", "error_params": {"status": 403}})
assert result == state.get_task("translated")
assert result["error_key"] == "codex.oauth.http_error"
assert result["error_params"] == {"status": 403}
|