test_unit.py
python
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360
Land Scooling Lab T0/T2 training contract and seven-tier tests
Human
minor
⚠ breaking
43 days ago
| 1 | """Unit tier tests for Scooling Lab T0/T2 contracts.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import unittest |
| 6 | |
| 7 | from scooling_lab_helpers import valid_payload |
| 8 | |
| 9 | from scooling_lab.contracts import TrainingJobRequest, TrainingJobStatus |
| 10 | from scooling_lab.errors import ApiError, ErrorCode, error_payload |
| 11 | from scooling_lab.license_policy import BomEntry, LicensePolicyError, validate_entry |
| 12 | from scooling_lab.state_machine import transition |
| 13 | |
| 14 | |
| 15 | class ScoolingLabUnitTests(unittest.TestCase): |
| 16 | """Unit tests for state transitions, schema, license policy, and errors.""" |
| 17 | |
| 18 | def test_unit_state_transitions_are_explicit_and_idempotent(self) -> None: |
| 19 | """queued -> running -> succeeded works and replay is a no-op.""" |
| 20 | |
| 21 | self.assertEqual( |
| 22 | transition(TrainingJobStatus.QUEUED, TrainingJobStatus.RUNNING), |
| 23 | TrainingJobStatus.RUNNING, |
| 24 | ) |
| 25 | self.assertEqual( |
| 26 | transition(TrainingJobStatus.RUNNING, TrainingJobStatus.SUCCEEDED), |
| 27 | TrainingJobStatus.SUCCEEDED, |
| 28 | ) |
| 29 | self.assertEqual( |
| 30 | transition(TrainingJobStatus.SUCCEEDED, TrainingJobStatus.SUCCEEDED), |
| 31 | TrainingJobStatus.SUCCEEDED, |
| 32 | ) |
| 33 | |
| 34 | def test_unit_invalid_state_transition_raises_safe_error(self) -> None: |
| 35 | """queued -> succeeded is not allowed by the transition table.""" |
| 36 | |
| 37 | with self.assertRaises(ApiError) as raised: |
| 38 | transition(TrainingJobStatus.QUEUED, TrainingJobStatus.SUCCEEDED) |
| 39 | self.assertEqual(raised.exception.code, ErrorCode.INVALID_TRANSITION) |
| 40 | |
| 41 | def test_unit_schema_rejects_unsafe_fields_and_unapproved_models(self) -> None: |
| 42 | """Dangerous keys and unknown model ids fail in schema validation.""" |
| 43 | |
| 44 | payload = valid_payload() |
| 45 | payload["workerUrl"] = "https://attacker.invalid/worker" |
| 46 | with self.assertRaises(ApiError): |
| 47 | TrainingJobRequest.from_mapping(payload) |
| 48 | |
| 49 | model_payload = valid_payload() |
| 50 | model_payload["modelId"] = "unapproved-model" |
| 51 | with self.assertRaises(ApiError): |
| 52 | TrainingJobRequest.from_mapping(model_payload) |
| 53 | |
| 54 | def test_unit_license_policy_blocks_agpl_and_blocked_paths(self) -> None: |
| 55 | """The BOM policy rejects AGPL licenses and Unsloth Studio/CLI paths.""" |
| 56 | |
| 57 | validate_entry( |
| 58 | BomEntry( |
| 59 | name="safe", |
| 60 | version="1.0.0", |
| 61 | license="MIT", |
| 62 | source_path="third_party/safe", |
| 63 | evidence="fixture", |
| 64 | ) |
| 65 | ) |
| 66 | with self.assertRaises(LicensePolicyError): |
| 67 | validate_entry( |
| 68 | BomEntry( |
| 69 | name="blocked", |
| 70 | version="1.0.0", |
| 71 | license="AGPL-3.0", |
| 72 | source_path="third_party/blocked", |
| 73 | evidence="fixture", |
| 74 | ) |
| 75 | ) |
| 76 | with self.assertRaises(LicensePolicyError): |
| 77 | validate_entry( |
| 78 | BomEntry( |
| 79 | name="blocked-path", |
| 80 | version="1.0.0", |
| 81 | license="Apache-2.0", |
| 82 | source_path="vendor/unsloth_cli/main.py", |
| 83 | evidence="fixture", |
| 84 | ) |
| 85 | ) |
| 86 | |
| 87 | def test_unit_error_model_does_not_echo_payload_or_paths(self) -> None: |
| 88 | """Safe errors expose only code and stable public message.""" |
| 89 | |
| 90 | payload = error_payload(ApiError(ErrorCode.VALIDATION_ERROR, 400)) |
| 91 | text = str(payload) |
| 92 | self.assertIn("VALIDATION_ERROR", text) |
| 93 | self.assertNotIn("/Users/", text) |
| 94 | self.assertNotIn("attacker.invalid", text) |
| 95 | self.assertNotIn("payload", text.lower()) |
| 96 | |
| 97 | |
| 98 | if __name__ == "__main__": |
| 99 | unittest.main() |
File History
1 commit
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360
Land Scooling Lab T0/T2 training contract and seven-tier tests
Human
minor
⚠
43 days ago