import tempfile import unittest from pathlib import Path from unittest.mock import patch import candidate_queue as queue import release_handoff as h class ReleaseHandoffTest(unittest.TestCase): def setUp(self): self.task = {'id': 'task'} self.review = {'id': 'comment', 'taskId': 'task', 'createdAt': '2', 'author': {'id': h.WHITE}, 'content': '🚦 可测试 ✅|[任务](mention://task/task)\n落地 `abcdef1`'} def test_latest_passing_review_and_task_identity(self): self.assertEqual(h.review_identity(self.task, [self.review], 'comment')[0], 'abcdef1') for invalid in (dict(self.review, author={'id': 'other'}), dict(self.review, taskId='other'), dict(self.review, content='🚦 不可测试,引用「可测试」')): with self.assertRaises(h.cd.CDError): h.review_identity(self.task, [invalid], 'comment') newer = dict(self.review, id='new', createdAt='3', content='🚦 未通过') with self.assertRaises(h.cd.CDError): h.review_identity(self.task, [self.review, newer], 'comment') def test_plain_verdict_resolves_each_reports_own_commit(self): for title, sha in [('可测试', 'ea20abc'), ('🚦 ✅ 可测试', 'abc123def')]: row = dict(self.review, content=f'{title}|[任务](mention://task/task)\n落地合并 `{sha}`') self.assertEqual(h.review_identity(self.task, [row], row['id'])[0], sha) for title in ['不可测试', '是否可测试?', '未通过,引用「可测试」']: row = dict(self.review, content=f'{title}|[任务](mention://task/task)\n落地合并 `ea20abc`') with self.assertRaises(h.cd.CDError): h.review_identity(self.task, [row], row['id']) def test_landed_commit_marker_in_merge_chain(self): header = self.review['content'].splitlines()[0] for marker in ('落地', '落地(merge)', '落地 merge'): row = dict(self.review, content=f'{header}\n源 `b2248a9` ➔ {marker} `d76363c`(11:56)➔ main `c90cb80`') self.assertEqual(h.review_identity(self.task, [row], 'comment')[0], 'd76363c') for body, error in [('源 `b2248a9`', 'missing landed commit'), ('落地 merge `d76363c`,落地 `c90cb80`', 'multiple landed commits')]: row = dict(self.review, content=f'{header}\n{body}') with self.assertRaisesRegex(h.cd.CDError, error): h.review_identity(self.task, [row], 'comment') def test_busy_candidate_waits_on_original_task_without_dispatch(self): with tempfile.TemporaryDirectory() as tmp, patch.object(h.coordinator, 'STATE', Path(tmp)), \ patch.object(h, 'resolve', return_value=('a' * 40, '30')), \ patch.object(h.coordinator, 'add_comment'), patch.object(h.coordinator, 'queue') as dispatch: task_id = '00000001-1111-2222-3333-444444444444' event = '00000002-1111-2222-3333-444444444444' slot = Path(tmp) / 'another' / 'compose.json' slot.parent.mkdir() slot.touch() first = h.preflight({'id': task_id}, event) second = h.preflight({'id': task_id}, event) self.assertEqual(first, second) self.assertEqual(first['state'], 'waiting_for_slot') self.assertEqual(len(list((Path(tmp) / 'waiting').glob('*.json'))), 1) dispatch.assert_not_called() def test_waiter_dispatches_once_when_slot_is_free(self): with tempfile.TemporaryDirectory() as tmp, patch.object(h.coordinator, 'STATE', Path(tmp)), \ patch.object(h, 'resolve', return_value=('a' * 40, '30')), \ patch.object(h.assistant, 'task_record', return_value={'id': 'task'}), \ patch.object(h.assistant.worker, 'main_site_sha', return_value='b' * 40), \ patch.object(h.candidate, 'git', return_value='b' * 40), \ patch.object(h.coordinator, 'add_comment'), \ patch.object(h, 'prepare_from_review', return_value={'url': 'https://example.test/run/1'}) as build: h.preflight({'id': 'task'}, 'event') self.assertEqual(queue.drain(), 'dispatched') self.assertEqual(queue.drain(), 'idle') build.assert_called_once_with({'id': 'task'}, 'event') def test_active_qa_slot_is_preserved(self): with tempfile.TemporaryDirectory() as tmp, patch.object(h.coordinator, 'STATE', Path(tmp)), \ patch.object(h.assistant, 'task_record', return_value={'status': {'id': 'testing'}}), \ patch.object(h.cd, 'run') as docker: root = Path(tmp) (root / 'waiting').mkdir() h.cd.record(root / 'waiting/event.json', {'state': 'waiting_for_slot'}) (root / 'slot').mkdir() (root / 'slot/compose.json').touch() h.cd.record(root / 'slot/ready.json', {'task': 'task'}) self.assertEqual(queue.drain(), 'occupied') docker.assert_not_called() def test_ready_demand_waits_for_owner_to_start_testing(self): record = {'id': 'task', 'typeId': '1b7f1bb1-72c2-4106-b305-85604cf6d7e2', 'url': 'https://example.test/task', 'status': {'id': h.coordinator.WAIT_TEST}} plan = {'task': 'task', 'items': [{'task': 'task'}]} with patch.object(h.coordinator, 'task', return_value=record), patch.object(h.coordinator, 'add_comment'), \ patch.object(h.coordinator.hfx, 'hifox') as cli: h.coordinator.publish_candidate(plan, dict(sha='a'*40, bundle='b'*64, url='https://example.test'), '1') cli.assert_not_called() def test_owner_approval_is_bound_to_exact_preview_and_parent(self): with tempfile.TemporaryDirectory() as tmp, patch.object(h, 'DIRECTORY', Path(tmp)): saved = {'content': 'reviewed scope', 'comment': 'preview', 'plan': {'frozen': True}} h.cd.record(Path(tmp) / 'task.preview.json', saved) parent = {'id': 'preview', 'content': saved['content']} reply = {'id': 'ok', 'createdAt': '2', 'content': '确认范围', 'parentId': 'preview', 'author': {'id': h.cd.RELEASE_OWNER, 'type': 'human'}} with patch.object(h, 'comments', return_value=[parent, reply]): self.assertEqual(h.frozen_approval('task'), saved['plan']) quoted = dict(reply, content='「确认范围」') with patch.object(h, 'comments', return_value=[parent, quoted]): self.assertEqual(h.frozen_approval('task', 'ok'), saved['plan']) self.assertIsNone(h.frozen_approval('task', 'other')) for bad in (dict(reply, parentId=None), dict(reply, runId='agent-run'), dict(reply, content='先不确认范围')): with patch.object(h, 'comments', return_value=[parent, bad]): self.assertIsNone(h.frozen_approval('task')) with patch.object(h, 'comments', return_value=[dict(parent, content='changed'), reply]), self.assertRaises(h.cd.CDError): h.frozen_approval('task')