Spaces:
Sleeping
Sleeping
Download src/tests/runtime/test_key_modules.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 8.13 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_key_modules.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/tests/runtime/test_key_modules.py
-
curl -L -o test_key_modules.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_key_modules.py
8.13 kB
| import json | |
| import unittest | |
| from pathlib import Path | |
| from src.modules.skill.domain.models import SkillError, digest, Draft | |
| from src.modules.context.service import ContextService | |
| from src.modules.project.service import ProjectAccess | |
| from src.modules.artifact_catalog.service import ArtifactCatalog | |
| from src.adapters.persistence.artifact_sqlite import SqliteArtifactRepository | |
| from src.modules.governance.local_validation import LocalValidation | |
| from src.adapters.llm.workos_credentials import workos_provider | |
| from src.tests.runtime.fixtures import setup, workspace, AUTHOR, USER, OTHER, REVIEWER, through_execution | |
| class KeyModulesTests(unittest.TestCase): | |
| def stage(self, runtime, authorization): | |
| doc = runtime.skills.read_draft(AUTHOR, 'erp-java-development') | |
| body = {k: v for k, v in doc['body'].items() if k not in {'owner_id', 'last_editor_id'}} | |
| draft = Draft.model_validate(body).model_copy(update={'project_id': 'fixture-project'}) | |
| runtime.skills.save_draft(AUTHOR, draft, doc['revision']) | |
| return runtime.skills.stage_local_validation(AUTHOR, draft.skill_id, authorization) | |
| def test_context_is_detached_and_binds_project_source_release(self): | |
| doc = {'requirement': 'x'} | |
| service = ContextService() | |
| a = service.snapshot(doc, [], project_id='a', source_digest='1' * 64, release_digest='2' * 64) | |
| b = service.snapshot(doc, [], project_id='b', source_digest='1' * 64, release_digest='2' * 64) | |
| doc['requirement'] = 'changed' | |
| self.assertEqual(a['document']['requirement'], 'x') | |
| self.assertNotEqual(a['digest'], b['digest']) | |
| with self.assertRaises(SkillError): | |
| service.snapshot({'requirement': 'x' * 18000}, [], project_id='a', source_digest='1', release_digest='2') | |
| def test_project_admin_does_not_imply_membership(self): | |
| access = ProjectAccess({(USER.tenant_id, 'p'): [USER.user_id]}) | |
| access(USER, 'p') | |
| for actor in [AUTHOR, OTHER]: | |
| with self.assertRaises(SkillError): | |
| access(actor, 'p') | |
| def test_artifact_idempotence_tenant_boundary_and_corruption(self): | |
| with workspace() as folder: | |
| catalog = ArtifactCatalog(SqliteArtifactRepository(Path(folder) / 'evidence.sqlite')) | |
| run_id = 'run-' + 'a' * 32 | |
| ref = catalog.record('tenant-a', run_id, {'source_digest': '1' * 64}) | |
| self.assertEqual(ref, catalog.record('tenant-a', run_id, {'source_digest': '1' * 64})) | |
| with self.assertRaises(SkillError): | |
| catalog.read('tenant-b', run_id, ref['digest']) | |
| import sqlite3 | |
| with sqlite3.connect(catalog.repository.path) as db: | |
| db.execute("UPDATE artifacts SET body='{}'") | |
| with self.assertRaises(SkillError) as err: | |
| catalog.read('tenant-a', run_id, ref['digest']) | |
| self.assertEqual(err.exception.code, 'ARTIFACT_DIGEST_MISMATCH') | |
| def test_fixed_tool_dispatch_and_source_validation(self): | |
| runtime, request, _, sandbox = setup(self.enterContext(workspace())) | |
| run = runtime.create(USER, request, 'tool-guard-key') | |
| with self.assertRaises(SkillError): | |
| runtime.tools.invoke('shell', run, {'command': 'untrusted'}, lambda: False) | |
| with self.assertRaises(SkillError): | |
| runtime.tools.invoke('AO08', run, {'source': {'files': {'../bad.java': 'x'}, 'source_digest': 'x'}}, lambda: False) | |
| self.assertEqual(sandbox.calls, []) | |
| def test_local_version_does_not_publish_or_allow_other_user(self): | |
| runtime, request, _, _ = setup(self.enterContext(workspace())) | |
| with self.assertRaises(SkillError) as err: | |
| runtime.skills.stage_local_validation(AUTHOR, 'erp-java-development', 'user-delegation') | |
| self.assertEqual(err.exception.code, 'VALIDATION_PROJECT_REQUIRED') | |
| ref = self.stage(runtime, 'user-delegation') | |
| self.assertIsNone(ref['body']['approval_id']) | |
| self.assertFalse(ref['body']['human_review']) | |
| self.assertNotIn(ref['key'], [x['key'] for x in runtime.skills.releases(AUTHOR)]) | |
| with self.assertRaises(SkillError): | |
| runtime.create(USER, dict(request, release_ref=ref['key']), 'local-private-key') | |
| with self.assertRaises(SkillError): | |
| runtime.skills.stage_local_validation(USER, 'erp-java-development', 'user-delegation') | |
| def test_delegation_stops_at_acceptance_and_archives_without_self_approval(self): | |
| folder = self.enterContext(workspace()) | |
| runtime, request, _, _ = setup(folder) | |
| runtime.artifacts = ArtifactCatalog(SqliteArtifactRepository(Path(folder) / 'artifacts.sqlite')) | |
| ref = self.stage(runtime, 'user-delegation') | |
| run = runtime.create(AUTHOR, dict(request, release_ref=ref['key']), 'delegation-key') | |
| driver = LocalValidation(runtime, AUTHOR, 'user-delegation') | |
| for _ in range(5): | |
| run = driver.step(run['id']) | |
| self.assertEqual(run['pending_approval']['scope'], 'acceptance') | |
| self.assertEqual(driver.step(run['id'])['status'], 'waiting_approval') | |
| evidence = runtime.validation_evidence(AUTHOR, run['id']) | |
| self.assertEqual(evidence['manifest']['status'], 'verified_pending_user_acceptance') | |
| self.assertFalse(evidence['manifest']['human_acceptance']) | |
| self.assertEqual(len(evidence['manifest']['approvals']), 2) | |
| self.assertTrue(all(x['mechanism'] == 'delegated_local_validation' for x in evidence['manifest']['approvals'])) | |
| with self.assertRaises(SkillError): | |
| runtime.approve(AUTHOR, run['id'], run['pending_approval']['digest'], delegation='user-delegation') | |
| def test_wrong_delegation_and_premature_evidence_denied(self): | |
| runtime, request, _, _ = setup(self.enterContext(workspace())) | |
| ref = self.stage(runtime, 'scope-a') | |
| run = runtime.create(AUTHOR, dict(request, release_ref=ref['key']), 'delegation-key') | |
| with self.assertRaises(SkillError): | |
| runtime.validation_evidence(AUTHOR, run['id']) | |
| runtime.advance(AUTHOR, run['id']) | |
| with self.assertRaises(SkillError): | |
| LocalValidation(runtime, AUTHOR, 'scope-b').step(run['id']) | |
| def test_readiness_requires_admin_and_does_not_claim_live_health(self): | |
| runtime, _, _, _ = setup(self.enterContext(workspace())) | |
| with self.assertRaises(SkillError): | |
| runtime.readiness(USER) | |
| self.assertFalse(runtime.readiness(AUTHOR)['production_accepted']) | |
| self.assertEqual(runtime.readiness(AUTHOR)['live_provider_health'], 'not_probed') | |
| class CredentialTests(unittest.TestCase): | |
| def test_named_non_sk_token_wins_over_comment_example(self): | |
| with workspace() as folder: | |
| path = Path(folder) / 'fixture.env' | |
| path.write_text('DASHSCOPE_API_KEY=synthetic-provider-token\n# example: sk-example-not-real\n') | |
| provider = workos_provider(path) | |
| self.assertEqual(provider._token, 'synthetic-provider-token') | |
| self.assertFalse(provider.enable_thinking) | |
| self.assertEqual(path.read_text(), 'DASHSCOPE_API_KEY=synthetic-provider-token\n# example: sk-example-not-real\n') | |
| def test_ambiguous_keys_and_foreign_host_rejected_without_secret(self): | |
| with workspace() as folder: | |
| path = Path(folder) / 'fixture.env' | |
| for content in ['sk-oneexample sk-twoexample', | |
| 'DASHSCOPE_API_KEY=synthetic-secret\nDASHSCOPE_BASE_URL=https://evil.example/compatible-mode/v1']: | |
| path.write_text(content) | |
| with self.assertRaises(ValueError) as err: | |
| workos_provider(path) | |
| self.assertEqual(str(err.exception), 'WORKOS_CREDENTIAL_CONFIGURATION_INVALID') | |
| def test_named_token_is_not_replaced_when_invalid(self): | |
| with workspace() as folder: | |
| path = Path(folder) / 'fixture.env' | |
| path.write_text('DASHSCOPE_API_KEY=not a valid token\n# sk-example') | |
| with self.assertRaises(ValueError): | |
| workos_provider(path) | |