"""Targeted tests of the read adapter; synthetic fixtures are not browser evidence.""" import json import unittest from types import SimpleNamespace from fastapi import FastAPI from fastapi.responses import JSONResponse from fastapi.testclient import TestClient from src.apps.workbench.workflow_read import WorkflowRead, build_router, safe from src.modules.skill.domain.models import Actor, Draft, SkillError from src.tests.runtime.fixtures import AUTHOR, REVIEWER, USER, OTHER, setup, through_execution, workspace class WorkflowReadTests(unittest.TestCase): def setUp(self): folder = self.enterContext(workspace()) self.runtime, self.request, self.model, self.sandbox = setup(folder) self.skills = self.runtime.skills self.read = WorkflowRead(SimpleNamespace(skills=self.skills, store=self.runtime.store, runtimes={'erp': self.runtime})) self.id = self.request['release_ref'].split('@')[0] def test_graph_comes_from_real_saved_definition(self): doc = self.skills.read_draft(AUTHOR, self.id) result = self.read.definition(AUTHOR, self.id) self.assertEqual(result['definition']['graph'], doc['body']['graph']) self.assertEqual(result['source']['digest'], doc['digest']) self.assertEqual(len(result['nodes']), 16) self.assertEqual(result['issues'], []) for node in result['nodes']: if node['definition']['kind'] == 'operation': self.assertEqual(node['definition']['operation_ref'], node['atom']['operation_id']+'@'+node['atom']['version']) else: self.assertIsNone(node['atom']['version']) def test_historical_run_does_not_use_changed_draft_or_current_registry(self): run = self.runtime.create(USER, self.request, 'readonly-history-0001') doc = self.skills.read_draft(AUTHOR, self.id) body = {k: v for k, v in doc['body'].items() if k not in {'owner_id', 'last_editor_id'}} body['graph']['nodes'][1]['label'] = 'Changed current draft' self.skills.save_draft(AUTHOR, Draft.model_validate(body), doc['revision']) with self.skills.repository.transaction(write=True) as tx: tx.db.execute("UPDATE skill_documents SET body=replace(body,'parse_requirements','changed_registry') WHERE kind='operation'") historical = self.read.definition(AUTHOR, self.id, run_id=run['id']) self.assertEqual(historical['definition']['graph'], run['release']['draft']['graph']) self.assertNotEqual(historical['definition']['graph'], self.read.definition(AUTHOR, self.id)['definition']['graph']) atom = next(n['atom'] for n in historical['nodes'] if n['definition'].get('operation_ref') == 'AO01@0.1.0') self.assertEqual(atom['handler_ref'], 'parse_requirements') def test_waiting_acceptance_is_not_success_and_output_without_events_is_recorded(self): run = through_execution(self.runtime, self.request) trace = self.read.trace(AUTHOR, run['id']) self.assertEqual(trace['status'], 'waiting_approval') self.assertEqual(trace['nodes']['acceptance']['status'], 'waiting') self.assertEqual(trace['nodes']['evidence']['status'], 'not_started') self.assertEqual(trace['nodes']['end']['status'], 'not_started') self.runtime.store.mutate(run['id'], lambda r: r['outputs'].update({'end': {'manifest': 'synthetic'}})) self.assertEqual(self.read.trace(AUTHOR, run['id'])['nodes']['end']['status'], 'recorded') def test_unknown_fields_and_unmappable_atoms_are_visible_not_dropped(self): with self.skills.repository.transaction(write=True) as tx: doc = tx.get(AUTHOR.tenant_id, 'draft', self.id) body = doc['body'] body['graph']['nodes'][1]['operation_ref'] = 'AO99@9.0.0' body['graph']['nodes'][1]['future_condition'] = {'branch': 'unsupported', 'api_key': 'DO_NOT_EXPOSE'} tx.update(AUTHOR.tenant_id, 'draft', self.id, body, doc['revision']) result = self.read.definition(AUTHOR, self.id) self.assertTrue(result['issues']) self.assertIn('future_condition', result['definition']['graph']['nodes'][1]) self.assertEqual(result['definition']['graph']['nodes'][1]['future_condition']['api_key'], '[已隐藏]') self.assertTrue(result['nodes'][1]['missing']) def test_permissions_and_project_access_apply_to_every_read(self): for call in (lambda: self.read.workflows(USER), lambda: self.read.atoms(USER), lambda: self.read.runs(USER), lambda: self.read.definition(USER, self.id)): with self.assertRaises(SkillError) as denied: call() self.assertEqual(denied.exception.status, 403) run = self.runtime.create(USER, self.request, 'readonly-acl-0001') with self.assertRaises(SkillError): self.read.trace(OTHER, run['id']) with self.skills.repository.transaction(write=True) as tx: doc = tx.get(AUTHOR.tenant_id, 'draft', self.id) tx.update(AUTHOR.tenant_id, 'draft', self.id, {**doc['body'], 'project_id': 'denied'}, doc['revision']) def deny(actor, project): raise SkillError('PROJECT_DENIED', 'denied', 403) self.skills.project_access = deny self.assertEqual(self.read.workflows(AUTHOR), []) self.assertEqual(self.read.runs(AUTHOR)['items'], []) with self.assertRaises(SkillError): self.read.definition(AUTHOR, self.id) def test_log_pagination_after_1000_and_error_location_and_secrets(self): run = self.runtime.create(USER, self.request, 'readonly-events-0001') with self.runtime.store.connect() as db: db.executemany('INSERT INTO runtime_stage_events(run_id,timestamp,stage,status,detail) VALUES(?,?,?,?,?)', [(run['id'], i, 'code', 'active', 'api_key=DO_NOT_EXPOSE') for i in range(1005)]) db.executemany('INSERT INTO runtime_stage_events(run_id,timestamp,stage,status,detail) VALUES(?,?,?,?,?)', [(run['id'], 1006, 'build', 'started', ''), (run['id'], 1008, 'build', 'failed', 'JAVA_BUILD_FAILED')]) after, count, pages = 0, 0, [] while True: trace = self.read.trace(AUTHOR, run['id'], after=after, limit=100) pages.append(trace['events']); count += len(trace['events']) if trace['after'] is None: break after = trace['after'] self.assertEqual(count, 1007) self.assertEqual(trace['nodes']['build']['error'], 'JAVA_BUILD_FAILED') self.assertEqual(trace['nodes']['build']['attempts'], [{'status': 'failed', 'seconds': 2}]) self.assertNotIn('DO_NOT_EXPOSE', json.dumps(pages)) def test_all_read_routes_have_no_writes_and_do_not_create_releases(self): app = FastAPI() app.include_router(build_router(self.read.engine, lambda: AUTHOR)) app.exception_handler(SkillError)(lambda request, exc: JSONResponse({'code': exc.code}, status_code=exc.status)) with self.skills.repository.transaction() as tx: before = [tuple(r) for r in tx.db.execute('SELECT * FROM skill_documents')] audits = tx.db.execute('SELECT count(*) FROM skill_audit').fetchone()[0] client = TestClient(app) for path in ('workflows', 'workflows/'+self.id, 'workflow-atoms', 'workflow-runs'): self.assertEqual(client.get('/api/v1/admin/'+path).status_code, 200) self.assertEqual(client.post('/api/v1/admin/'+path).status_code, 405) self.assertEqual(client.get('/api/v1/admin/workflow-runs?limit=101').status_code, 422) with self.skills.repository.transaction() as tx: self.assertEqual(before, [tuple(r) for r in tx.db.execute('SELECT * FROM skill_documents')]) self.assertEqual(audits, tx.db.execute('SELECT count(*) FROM skill_audit').fetchone()[0]) def test_atom_catalog_has_exact_version_references_and_real_system_types(self): catalog = self.read.atoms(AUTHOR) ao = next(a for a in catalog if a['ref'] == 'AO05@0.1.0') self.assertTrue(any(r['node'] == 'code' and r['workflow'] == self.id for r in ao['references'])) system = next(a for a in catalog if a['ref'] == 'system:approval:design') self.assertIsNone(system['version']) self.assertEqual(safe({'credential_ref': 'cred-123', 'api_key': 'secret'}), {'credential_ref': 'cred-123', 'api_key': '[已隐藏]'}) def test_nonempty_run_list_paginates_without_holding_read_cursor_during_acl(self): ids = [self.runtime.create(USER, self.request, 'readonly-list-'+str(i))['id'] for i in range(3)] first = self.read.runs(AUTHOR, limit=2) self.assertEqual([r['id'] for r in first['items']], ids[::-1][:2]) self.assertIsNotNone(first['before']) last = self.read.runs(AUTHOR, limit=2, before=first['before'], workflow=self.id) self.assertEqual([r['id'] for r in last['items']], ids[:1]) self.assertIsNone(last['before']) def test_expired_lease_is_exposed_without_rewriting_unknown_run(self): run = self.runtime.create(USER, self.request, 'readonly-expired-0001') self.runtime.store.mutate(run['id'], lambda r: r.update(status='running', lease_until=1)) self.assertTrue(self.read.runs(AUTHOR)['items'][0]['lease_expired']) self.assertTrue(self.read.trace(AUTHOR, run['id'])['lease_expired']) self.assertEqual(self.runtime.store.get(run['id'])['status'], 'running') if __name__ == '__main__': unittest.main()