Skip to content

Commit a997fc1

Browse files
codexmldangelo-oai
authored andcommitted
test(workbench): reuse workspace and lifecycle command fixtures
1 parent 015fd18 commit a997fc1

6 files changed

Lines changed: 254 additions & 424 deletions

File tree

‎plugins/codex-security/tests/test_scan_contract_examples.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from jsonschema import Draft202012Validator, FormatChecker
1111
from referencing import Registry, Resource
12+
from workbench_test_support import saved_coverage
1213

1314
PLUGIN_DIR = Path(__file__).resolve().parent.parent
1415
EXAMPLE_DIR = PLUGIN_DIR / "examples" / "completed-scan"
@@ -352,12 +353,7 @@ def test_findings_accept_code_evidence_call_stack_role(self) -> None:
352353
draft = {
353354
"scanId": "7fc17317-9594-49e0-b06a-d72fd7e14bba",
354355
"findings": [draft_finding],
355-
"coverage": {
356-
"completeness": "complete",
357-
"surfaces": [],
358-
"explicitExclusions": [],
359-
"deferred": [],
360-
},
356+
"coverage": saved_coverage(),
361357
}
362358
draft_validator.validate(draft)
363359

‎plugins/codex-security/tests/test_workbench_cancellation.py‎

Lines changed: 35 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
from pathlib import Path
55

66
from workbench_test_support import (
7+
cancel_scan,
78
create_saved_workspace,
8-
run_workbench,
9+
fail_scan,
10+
get_scan,
11+
mark_handoff_delivered,
12+
scan_claim_command,
13+
scan_command,
914
start_delivered_scan,
15+
start_scan_command,
1016
write_completed_contract,
1117
)
1218

@@ -16,39 +22,21 @@ def test_workbench_records_scan_failure(tmp_path: Path) -> None:
1622
target = tmp_path / "target"
1723
target.mkdir()
1824
saved = create_saved_workspace(state_dir, target)
19-
started = run_workbench(
20-
state_dir,
21-
"start-scan",
22-
"--workspace-id",
23-
str(saved["id"]),
24-
"--scan-root",
25-
str(tmp_path / "scans"),
25+
started = start_scan_command(
26+
state_dir, str(saved["id"]), "--scan-root", str(tmp_path / "scans")
2627
)
2728
scan_id = str(started["results"]["scanId"])
2829
claim_token = str(uuid.uuid4())
29-
run_workbench(
30-
state_dir, "claim-handoff-delivery", "--scan-id", scan_id, "--claim-token", claim_token
31-
)
32-
failed = run_workbench(
33-
state_dir,
34-
"fail-scan",
35-
"--scan-id",
36-
scan_id,
37-
"--message",
38-
"Repository checkout became unavailable.",
39-
"--claim-token",
40-
claim_token,
30+
scan_claim_command(state_dir, "claim-handoff-delivery", scan_id, claim_token)
31+
failed = fail_scan(
32+
state_dir, scan_id, "Repository checkout became unavailable.", "--claim-token", claim_token
4133
)
4234
assert failed["scan"]["progress"]["status"] == "failed"
4335
assert failed["scan"]["failureMessage"] == "Repository checkout became unavailable."
4436

45-
delivered = run_workbench(
46-
state_dir, "mark-handoff-delivered", "--scan-id", scan_id, "--claim-token", claim_token
47-
)
37+
delivered = mark_handoff_delivered(state_dir, scan_id, claim_token)
4838
assert delivered["results"]["handoffStatus"] == "delivered"
49-
replayed = run_workbench(
50-
state_dir, "mark-handoff-delivered", "--scan-id", scan_id, "--claim-token", claim_token
51-
)
39+
replayed = mark_handoff_delivered(state_dir, scan_id, claim_token)
5240
assert replayed["results"]["handoffStatus"] == "delivered"
5341

5442

@@ -58,84 +46,46 @@ def test_workbench_cancels_running_scan_and_rejects_late_updates(tmp_path: Path)
5846
target.mkdir()
5947
thread_id = "thread-cancel-owner"
6048
saved = create_saved_workspace(state_dir, target, thread_id=thread_id)
61-
started = run_workbench(state_dir, "start-scan", "--workspace-id", str(saved["id"]))
49+
started = start_scan_command(state_dir, str(saved["id"]))
6250
scan_id = str(started["results"]["scanId"])
6351
claim_token = str(uuid.uuid4())
64-
claimed = run_workbench(
65-
state_dir, "claim-handoff-delivery", "--scan-id", scan_id, "--claim-token", claim_token
66-
)
52+
claimed = scan_claim_command(state_dir, "claim-handoff-delivery", scan_id, claim_token)
6753
assert claimed["results"]["handoffClaimToken"] == claim_token
6854

69-
wrong_thread = run_workbench(
70-
state_dir,
71-
"cancel-scan",
72-
"--scan-id",
73-
scan_id,
74-
"--thread-id",
75-
"thread-cancel-other",
76-
check=False,
77-
)
55+
wrong_thread = cancel_scan(state_dir, scan_id, "thread-cancel-other", check=False)
7856
assert wrong_thread["returncode"] != 0
7957
assert "owning Codex thread" in str(wrong_thread["stderr"])
8058

81-
canceled = run_workbench(
82-
state_dir, "cancel-scan", "--scan-id", scan_id, "--thread-id", thread_id
83-
)
59+
canceled = cancel_scan(state_dir, scan_id, thread_id)
8460
assert canceled["results"]["progress"]["status"] == "canceled"
8561
assert canceled["results"]["canceledAt"]
8662
assert canceled["results"]["handoffClaimToken"] == claim_token
8763

88-
replayed = run_workbench(
89-
state_dir, "cancel-scan", "--scan-id", scan_id, "--thread-id", thread_id
90-
)
64+
replayed = cancel_scan(state_dir, scan_id, thread_id)
9165
assert replayed["results"]["canceledAt"] == canceled["results"]["canceledAt"]
9266

9367
for command in (
9468
("update-progress", "--phase", "discovery"),
9569
("complete-scan",),
9670
):
97-
rejected = run_workbench(
98-
state_dir,
99-
command[0],
100-
"--scan-id",
101-
scan_id,
102-
*command[1:],
103-
check=False,
104-
)
71+
rejected = scan_command(state_dir, command[0], scan_id, *command[1:], check=False)
10572
assert rejected["returncode"] != 0
10673

107-
delivered = run_workbench(
108-
state_dir,
109-
"mark-handoff-delivered",
110-
"--scan-id",
111-
scan_id,
112-
"--claim-token",
113-
claim_token,
114-
"--thread-id",
115-
thread_id,
116-
)
74+
delivered = mark_handoff_delivered(state_dir, scan_id, claim_token, "--thread-id", thread_id)
11775
assert delivered["results"]["progress"]["status"] == "canceled"
11876
assert delivered["results"]["handoffStatus"] == "delivered"
11977

12078
restarted = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
12179
restarted_scan_id = str(restarted["results"]["scanId"])
12280
assert restarted_scan_id != scan_id
12381
assert restarted["results"]["progress"]["status"] == "running"
124-
previous_scan = run_workbench(state_dir, "get-scan", "--scan-id", scan_id)
82+
previous_scan = get_scan(state_dir, scan_id)
12583
assert previous_scan["scan"]["scanId"] == scan_id
12684
assert previous_scan["workspace"]["results"]["scanId"] == scan_id
12785
assert previous_scan["workspace"]["results"]["progress"]["status"] == "canceled"
12886
write_completed_contract(Path(str(restarted["results"]["scanDir"])), restarted_scan_id, target)
129-
run_workbench(state_dir, "complete-scan", "--scan-id", restarted_scan_id)
130-
rejected = run_workbench(
131-
state_dir,
132-
"cancel-scan",
133-
"--scan-id",
134-
restarted_scan_id,
135-
"--thread-id",
136-
thread_id,
137-
check=False,
138-
)
87+
scan_command(state_dir, "complete-scan", restarted_scan_id)
88+
rejected = cancel_scan(state_dir, restarted_scan_id, thread_id, check=False)
13989
assert rejected["returncode"] != 0
14090
assert "Only a running scan can be canceled" in str(rejected["stderr"])
14191

@@ -146,50 +96,23 @@ def test_workbench_rejects_unconfirmed_cross_thread_handoff_delivery(tmp_path: P
14696
target.mkdir()
14797
thread_id = "thread-handoff-owner"
14898
saved = create_saved_workspace(state_dir, target, thread_id=thread_id)
149-
started = run_workbench(state_dir, "start-scan", "--workspace-id", str(saved["id"]))
99+
started = start_scan_command(state_dir, str(saved["id"]))
150100
scan_id = str(started["results"]["scanId"])
151101
claim_token = str(uuid.uuid4())
152-
run_workbench(
153-
state_dir, "claim-handoff-delivery", "--scan-id", scan_id, "--claim-token", claim_token
154-
)
102+
scan_claim_command(state_dir, "claim-handoff-delivery", scan_id, claim_token)
155103

156-
wrong_thread = run_workbench(
157-
state_dir,
158-
"mark-handoff-delivered",
159-
"--scan-id",
160-
scan_id,
161-
"--claim-token",
162-
claim_token,
163-
"--thread-id",
164-
"thread-handoff-other",
165-
check=False,
104+
wrong_thread = mark_handoff_delivered(
105+
state_dir, scan_id, claim_token, "--thread-id", "thread-handoff-other", check=False
166106
)
167107
assert wrong_thread["returncode"] != 0
168108
assert "owning Codex thread" in str(wrong_thread["stderr"])
169109

170-
delivered = run_workbench(
171-
state_dir,
172-
"mark-handoff-delivered",
173-
"--scan-id",
174-
scan_id,
175-
"--claim-token",
176-
claim_token,
177-
"--thread-id",
178-
thread_id,
179-
)
110+
delivered = mark_handoff_delivered(state_dir, scan_id, claim_token, "--thread-id", thread_id)
180111
assert delivered["results"]["handoffStatus"] == "delivered"
181112
assert delivered["results"]["handoffClaimToken"] == claim_token
182113

183-
wrong_replay = run_workbench(
184-
state_dir,
185-
"mark-handoff-delivered",
186-
"--scan-id",
187-
scan_id,
188-
"--claim-token",
189-
str(uuid.uuid4()),
190-
"--thread-id",
191-
thread_id,
192-
check=False,
114+
wrong_replay = mark_handoff_delivered(
115+
state_dir, scan_id, str(uuid.uuid4()), "--thread-id", thread_id, check=False
193116
)
194117
assert wrong_replay["returncode"] != 0
195118
assert "owned by another continuation" in str(wrong_replay["stderr"])
@@ -200,22 +123,13 @@ def test_workbench_allows_user_confirmed_recovery_in_another_thread(tmp_path: Pa
200123
target = tmp_path / "target"
201124
target.mkdir()
202125
saved = create_saved_workspace(state_dir, target, thread_id="thread-recovery-owner")
203-
started = run_workbench(state_dir, "start-scan", "--workspace-id", str(saved["id"]))
126+
started = start_scan_command(state_dir, str(saved["id"]))
204127
scan_id = str(started["results"]["scanId"])
205128
claim_token = f"recovery_{uuid.uuid4()}"
206-
run_workbench(
207-
state_dir, "claim-handoff-delivery", "--scan-id", scan_id, "--claim-token", claim_token
208-
)
129+
scan_claim_command(state_dir, "claim-handoff-delivery", scan_id, claim_token)
209130

210-
delivered = run_workbench(
211-
state_dir,
212-
"mark-handoff-delivered",
213-
"--scan-id",
214-
scan_id,
215-
"--claim-token",
216-
claim_token,
217-
"--thread-id",
218-
"thread-recovery-confirmed",
131+
delivered = mark_handoff_delivered(
132+
state_dir, scan_id, claim_token, "--thread-id", "thread-recovery-confirmed"
219133
)
220134

221135
assert delivered["results"]["handoffStatus"] == "delivered"

0 commit comments

Comments
 (0)