"""Editable Skill settings must survive saving and freeze into real task inputs.""" import json import unittest from pathlib import Path from pydantic import ValidationError from src.modules.skill.domain.models import Draft, SkillError, digest from src.modules.llm_gateway.service import LLMGateway, ModelReply from src.tests.runtime.fixtures import setup, workspace, AUTHOR, USER class SkillEditorTests(unittest.TestCase): def setUp(self): self.root=Path(self.enterContext(workspace())) self.runtime,self.request,_,_=setup(self.root) self.service=self.runtime.skills self.key='erp-java-development' original=self.service.read_draft(AUTHOR,self.key) data={k:v for k,v in original['body'].items() if k not in {'owner_id','last_editor_id'}} self.draft=Draft.model_validate({**data,'project_id':'fixture-project', 'instructions':'页面使用中文;验收时核对实际订单状态。', 'editor_positions':{'start':{'x':120,'y':80}}}) self.saved=self.service.save_draft(AUTHOR,self.draft,original['revision']) def test_settings_persist_and_old_snapshot_does_not_follow_later_edits(self): self.assertEqual(self.service.read_draft(AUTHOR,self.key)['body']['editor_positions']['start'],{'x':120.0,'y':80.0}) release=self.service.stage_local_validation(AUTHOR,self.key,'unit-explicit-local-test',validation_owner=USER.user_id,expected_revision=self.saved['revision']) self.assertEqual(release['body']['draft_digest'],self.saved['digest']) self.assertEqual(release['body']['draft_revision'],self.saved['revision']) self.service.save_draft(AUTHOR,self.draft.model_copy(update={'instructions':'后来修改的指导'}),self.saved['revision']) snap=self.service.prepare(USER,release['key'],'fixture-project',digest({}),'frozen-settings-test') self.assertEqual(snap['skill_instructions'],self.draft.instructions) with self.assertRaises(SkillError) as conflict: self.service.stage_local_validation(AUTHOR,self.key,'unit-explicit-local-test',expected_revision=self.saved['revision']) self.assertEqual(conflict.exception.code,'REVISION_CONFLICT') def test_model_receives_actual_saved_skill_guidance_without_new_permissions(self): release=self.service.stage_local_validation(AUTHOR,self.key,'unit-explicit-local-test',validation_owner=USER.user_id) request={**self.request,'release_ref':release['key']} run=self.runtime.create(USER,request,'editor-guidance-test') self.runtime.store.mutate(run['id'],lambda r:r.update(status='running',claim='unit-test')) seen=[] class Provider: name='synthetic-editor-provider' def complete(self,messages,max_tokens,cancelled): seen.extend(messages) return ModelReply({'summary':'Synthetic design'},10,10,'fixture-request') LLMGateway(self.runtime.store,[Provider()]).generate(run['id'],'design',{},lambda:False) self.assertEqual(json.loads(seen[-1]['content'])['skill_guidance'],self.draft.instructions) self.assertIn('cannot authorize tools',seen[0]['content']) self.assertEqual(self.runtime.store.get(run['id'])['budget'],run['budget']) def test_editor_cannot_save_ghost_nodes_or_expand_hard_budget(self): data=self.draft.model_dump() data['editor_positions']['missing-node']={'x':1,'y':2} with self.assertRaises(ValidationError):Draft.model_validate(data) data=self.draft.model_dump();data['budget']['max_model_calls']=7 with self.assertRaises(ValidationError):Draft.model_validate(data) if __name__=='__main__':unittest.main()