388 行
14 KiB
Python
388 行
14 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
|
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
|
|
|
|
|
def _repository(tmp_path: Path) -> tuple[Database, AdvancedMaterialRepository]:
|
|
database = Database(
|
|
DatabaseSettings(
|
|
backend="sqlite",
|
|
sqlite=SqliteSettings(path=str(tmp_path / "advanced-material-repository.sqlite3")),
|
|
)
|
|
)
|
|
database.initialize()
|
|
return database, AdvancedMaterialRepository(database)
|
|
|
|
|
|
def _completed_attempt(
|
|
repository: AdvancedMaterialRepository,
|
|
*,
|
|
import_id: str,
|
|
attempt_id: str,
|
|
) -> None:
|
|
repository.create_import(
|
|
{
|
|
"import_id": import_id,
|
|
"display_name": "verified red dress",
|
|
"source_image_uri": f"storage://advanced_materials/sources/{import_id}/source.png",
|
|
"source_thumbnail_uri": f"storage://advanced_materials/sources/{import_id}/source.png",
|
|
"status": "awaiting_review",
|
|
"recognition_prompt": "red silk dress, gold embroidery",
|
|
"recognition_negative_prompt": "watermark",
|
|
"recognition_tags": ["dress", "red"],
|
|
}
|
|
)
|
|
repository.create_attempt(
|
|
{
|
|
"attempt_id": attempt_id,
|
|
"import_id": import_id,
|
|
"status": "awaiting_review",
|
|
"prompt": "red silk dress, gold embroidery",
|
|
"negative_prompt": "watermark",
|
|
"validation_image_uri": (
|
|
f"storage://advanced_materials/attempts/{import_id}/{attempt_id}/validation.png"
|
|
),
|
|
"validation_thumbnail_uri": (
|
|
f"storage://advanced_materials/attempts/{import_id}/{attempt_id}/thumbnail.png"
|
|
),
|
|
}
|
|
)
|
|
|
|
|
|
def test_accept_attempt_is_idempotent_for_the_same_import_and_attempt(tmp_path: Path) -> None:
|
|
_database, repository = _repository(tmp_path)
|
|
_completed_attempt(repository, import_id="advimp_idempotent", attempt_id="attempt_1")
|
|
|
|
first = repository.accept_attempt(
|
|
"advimp_idempotent",
|
|
"attempt_1",
|
|
{
|
|
"material_id": "advmat_first",
|
|
"priority": 100,
|
|
"capsule": {"standard": {"prompt_parts": ["red silk dress"]}},
|
|
},
|
|
)
|
|
replay = repository.accept_attempt(
|
|
"advimp_idempotent",
|
|
"attempt_1",
|
|
{"material_id": "advmat_should_not_be_created", "priority": 1},
|
|
)
|
|
|
|
assert replay.material_id == first.material_id == "advmat_first"
|
|
assert replay.priority == 100
|
|
materials, total = repository.list_materials(page=1, page_size=10)
|
|
assert total == 1
|
|
assert [item.material_id for item in materials] == ["advmat_first"]
|
|
record = repository.get_import("advimp_idempotent")
|
|
attempt = repository.get_attempt("attempt_1")
|
|
assert record is not None and record.status == "accepted"
|
|
assert record.accepted_material_id == "advmat_first"
|
|
assert attempt is not None and attempt.status == "accepted"
|
|
|
|
|
|
def test_accept_attempt_rolls_back_material_attempt_and_import_together(tmp_path: Path) -> None:
|
|
database, repository = _repository(tmp_path)
|
|
_completed_attempt(repository, import_id="advimp_atomic", attempt_id="attempt_atomic")
|
|
connection = database.connect()
|
|
try:
|
|
connection.execute(
|
|
"""
|
|
CREATE TRIGGER fail_advanced_material_accept
|
|
BEFORE UPDATE OF status ON advanced_material_validation_attempts
|
|
WHEN NEW.attempt_id = 'attempt_atomic' AND NEW.status = 'accepted'
|
|
BEGIN
|
|
SELECT RAISE(ABORT, 'simulated advanced material accept failure');
|
|
END
|
|
"""
|
|
)
|
|
connection.commit()
|
|
finally:
|
|
connection.close()
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
repository.accept_attempt(
|
|
"advimp_atomic",
|
|
"attempt_atomic",
|
|
{"material_id": "advmat_atomic"},
|
|
)
|
|
|
|
assert repository.get_material("advmat_atomic") is None
|
|
record = repository.get_import("advimp_atomic")
|
|
attempt = repository.get_attempt("attempt_atomic")
|
|
assert record is not None and record.status == "awaiting_review"
|
|
assert record.accepted_material_id is None
|
|
assert attempt is not None and attempt.status == "awaiting_review"
|
|
assert attempt.decision is None
|
|
|
|
|
|
def test_advanced_style_sources_and_consensus_support_independent_completion(tmp_path: Path) -> None:
|
|
_database, repository = _repository(tmp_path)
|
|
repository.create_import(
|
|
{
|
|
"import_id": "advimp_style_set",
|
|
"display_name": "shared painted style",
|
|
"source_image_uri": "storage://advanced_materials/style-set/primary.png",
|
|
"status": "source_recognition_queued",
|
|
}
|
|
)
|
|
first = repository.create_style_source(
|
|
{
|
|
"source_id": "advsrc_1",
|
|
"import_id": "advimp_style_set",
|
|
"sort_order": 0,
|
|
"is_primary": True,
|
|
"source_image_uri": "storage://advanced_materials/style-set/1.png",
|
|
}
|
|
)
|
|
second = repository.create_style_source(
|
|
{
|
|
"source_id": "advsrc_2",
|
|
"import_id": "advimp_style_set",
|
|
"sort_order": 1,
|
|
"source_image_uri": "storage://advanced_materials/style-set/2.png",
|
|
}
|
|
)
|
|
|
|
assert first.is_primary is True
|
|
assert second.status == "queued"
|
|
running = repository.update_style_source_if_status(
|
|
"advsrc_1", {"queued"}, {"status": "running"}
|
|
)
|
|
assert running is not None and running.status == "running"
|
|
completed = repository.update_style_source_if_status(
|
|
"advsrc_1",
|
|
{"running"},
|
|
{
|
|
"status": "succeeded",
|
|
"recognition_prompt": "soft charcoal outlines, layered watercolor",
|
|
"recognition_tags": ["watercolor", "charcoal"],
|
|
"recognition_result": {"confidence": 0.91},
|
|
},
|
|
)
|
|
assert completed is not None and completed.completed_at is not None
|
|
assert repository.update_style_source_if_status(
|
|
"advsrc_1", {"queued"}, {"status": "running"}
|
|
) is None
|
|
assert [item.source_id for item in repository.list_style_sources("advimp_style_set")] == [
|
|
"advsrc_1",
|
|
"advsrc_2",
|
|
]
|
|
|
|
consensus = repository.create_style_consensus_run(
|
|
{
|
|
"consensus_id": "advconsensus_1",
|
|
"import_id": "advimp_style_set",
|
|
"revision": 1,
|
|
"source_ids": ["advsrc_1", "advsrc_2"],
|
|
"model_route": "material_vlm",
|
|
}
|
|
)
|
|
assert consensus.source_ids == ["advsrc_1", "advsrc_2"]
|
|
consensus = repository.update_style_consensus_run(
|
|
consensus.consensus_id,
|
|
{
|
|
"status": "succeeded",
|
|
"prompt": "soft charcoal outlines, translucent watercolor layers",
|
|
"tags": ["watercolor"],
|
|
"result": {"generation_capsules": {"detailed": {"prompt_parts": ["watercolor"]}}},
|
|
},
|
|
expected_statuses={"queued"},
|
|
)
|
|
assert consensus is not None and consensus.completed_at is not None
|
|
assert repository.get_latest_style_consensus_run("advimp_style_set") == consensus
|
|
|
|
|
|
def test_style_validation_batch_aggregates_three_cases_and_seals_one_material(tmp_path: Path) -> None:
|
|
_database, repository = _repository(tmp_path)
|
|
repository.create_import(
|
|
{
|
|
"import_id": "advimp_style_validation",
|
|
"display_name": "verified watercolor style",
|
|
"source_image_uri": "storage://advanced_materials/style-validation/primary.png",
|
|
"status": "ready_for_validation",
|
|
"recognition_prompt": "watercolor line art",
|
|
"recognition_negative_prompt": "photorealistic",
|
|
}
|
|
)
|
|
attempt, cases = repository.create_style_validation_batch(
|
|
{
|
|
"attempt_id": "advattempt_style_1",
|
|
"import_id": "advimp_style_validation",
|
|
"status": "running",
|
|
"prompt": "watercolor line art",
|
|
"negative_prompt": "photorealistic",
|
|
"model_route": "image_default",
|
|
},
|
|
[
|
|
{
|
|
"case_id": "advcase_portrait",
|
|
"case_key": "portrait",
|
|
"sort_order": 0,
|
|
"content_prompt": "elderly mechanic portrait",
|
|
"final_prompt": "watercolor line art, elderly mechanic portrait",
|
|
},
|
|
{
|
|
"case_id": "advcase_fullbody",
|
|
"case_key": "fullbody",
|
|
"sort_order": 1,
|
|
"content_prompt": "robot courier full body",
|
|
"final_prompt": "watercolor line art, robot courier full body",
|
|
},
|
|
{
|
|
"case_id": "advcase_scene",
|
|
"case_key": "scene",
|
|
"sort_order": 2,
|
|
"content_prompt": "stormy harbor crane scene",
|
|
"final_prompt": "watercolor line art, stormy harbor crane scene",
|
|
},
|
|
],
|
|
)
|
|
assert attempt.attempt_number == 1
|
|
assert [item.case_key for item in cases] == ["portrait", "fullbody", "scene"]
|
|
|
|
for item in cases[:2]:
|
|
repository.update_style_validation_case(
|
|
item.case_id,
|
|
{
|
|
"status": "succeeded",
|
|
"validation_image_uri": f"storage://advanced_materials/validation/{item.case_id}.png",
|
|
"validation_thumbnail_uri": f"storage://advanced_materials/validation/{item.case_id}-thumb.png",
|
|
"provider_id": "provider",
|
|
"provider_model": "image-model",
|
|
"width": 1024,
|
|
"height": 1024,
|
|
},
|
|
expected_statuses={"queued"},
|
|
)
|
|
repository.update_style_validation_case(
|
|
cases[2].case_id,
|
|
{"status": "failed", "error": {"error_type": "timeout"}},
|
|
expected_statuses={"queued"},
|
|
)
|
|
record, attempt, current_cases = repository.finalize_style_validation_batch_if_ready(
|
|
"advimp_style_validation", "advattempt_style_1"
|
|
)
|
|
assert record.status == "validation_partial_failed"
|
|
assert attempt.status == "validation_partial_failed"
|
|
assert current_cases[2].status == "failed"
|
|
with pytest.raises(ValueError, match="all three"):
|
|
repository.accept_attempt(
|
|
"advimp_style_validation",
|
|
"advattempt_style_1",
|
|
{"material_id": "advmat_too_early"},
|
|
)
|
|
|
|
repository.update_style_validation_case(
|
|
"advcase_scene", {"status": "running", "error": None}, expected_statuses={"failed"}
|
|
)
|
|
repository.update_style_validation_case(
|
|
"advcase_scene",
|
|
{
|
|
"status": "succeeded",
|
|
"validation_image_uri": "storage://advanced_materials/validation/scene.png",
|
|
"validation_thumbnail_uri": "storage://advanced_materials/validation/scene-thumb.png",
|
|
"width": 1024,
|
|
"height": 1024,
|
|
},
|
|
expected_statuses={"running"},
|
|
)
|
|
record, attempt, current_cases = repository.finalize_style_validation_batch_if_ready(
|
|
"advimp_style_validation", "advattempt_style_1"
|
|
)
|
|
assert record.status == "awaiting_review"
|
|
assert attempt.status == "awaiting_review"
|
|
assert attempt.validation_image_uri.endswith("advcase_portrait.png")
|
|
assert all(item.status == "succeeded" for item in current_cases)
|
|
|
|
material = repository.accept_attempt(
|
|
"advimp_style_validation",
|
|
"advattempt_style_1",
|
|
{
|
|
"material_id": "advmat_style_set",
|
|
"prompt": "watercolor line art",
|
|
"capsule": {"detailed": {"prompt_parts": ["watercolor line art"]}},
|
|
},
|
|
)
|
|
assert material.material_id == "advmat_style_set"
|
|
assert material.accepted_attempt_id == "advattempt_style_1"
|
|
|
|
|
|
def test_style_validation_batch_creation_is_atomic(tmp_path: Path) -> None:
|
|
_database, repository = _repository(tmp_path)
|
|
repository.create_import(
|
|
{
|
|
"import_id": "advimp_atomic_style_batch",
|
|
"display_name": "atomic style batch",
|
|
"source_image_uri": "storage://advanced_materials/atomic/primary.png",
|
|
"status": "ready_for_validation",
|
|
"recognition_prompt": "ink wash",
|
|
}
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="unique"):
|
|
repository.create_style_validation_batch(
|
|
{
|
|
"attempt_id": "advattempt_invalid_style_batch",
|
|
"import_id": "advimp_atomic_style_batch",
|
|
"prompt": "ink wash",
|
|
},
|
|
[
|
|
{"case_key": "portrait", "content_prompt": "a", "final_prompt": "ink wash, a"},
|
|
{"case_key": "portrait", "content_prompt": "b", "final_prompt": "ink wash, b"},
|
|
{"case_key": "scene", "content_prompt": "c", "final_prompt": "ink wash, c"},
|
|
],
|
|
)
|
|
|
|
assert repository.list_attempts("advimp_atomic_style_batch") == []
|
|
|
|
|
|
def test_style_import_and_all_sources_are_created_atomically(tmp_path: Path) -> None:
|
|
database, repository = _repository(tmp_path)
|
|
connection = database.connect()
|
|
try:
|
|
connection.execute(
|
|
"""
|
|
CREATE TRIGGER fail_second_style_source
|
|
BEFORE INSERT ON advanced_style_sources
|
|
WHEN NEW.import_id = 'advimp_atomic_sources' AND NEW.sort_order = 1
|
|
BEGIN
|
|
SELECT RAISE(ABORT, 'simulated style source failure');
|
|
END
|
|
"""
|
|
)
|
|
connection.commit()
|
|
finally:
|
|
connection.close()
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
repository.create_style_import_with_sources(
|
|
{
|
|
"import_id": "advimp_atomic_sources",
|
|
"display_name": "atomic sources",
|
|
"source_image_uri": "storage://advanced_materials/atomic-sources/1.png",
|
|
"metadata": {"workflow_kind": "style_set"},
|
|
},
|
|
[
|
|
{
|
|
"source_id": "advsrc_atomic_1",
|
|
"sort_order": 0,
|
|
"is_primary": True,
|
|
"source_image_uri": "storage://advanced_materials/atomic-sources/1.png",
|
|
},
|
|
{
|
|
"source_id": "advsrc_atomic_2",
|
|
"sort_order": 1,
|
|
"is_primary": False,
|
|
"source_image_uri": "storage://advanced_materials/atomic-sources/2.png",
|
|
},
|
|
],
|
|
)
|
|
|
|
assert repository.get_import("advimp_atomic_sources") is None
|
|
assert repository.list_style_sources("advimp_atomic_sources") == []
|