package net.intrepid.aiengineering; import net.intrepid.aiengineering.cli.EngineeringCli; import net.intrepid.aiengineering.work.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.*; import java.nio.file.*; import java.util.*; import static org.assertj.core.api.Assertions.*; class EpicWorkflowTest { @TempDir Path root; private String output; private int cli(String... args) { var out = new StringWriter(); var err = new StringWriter(); int code = new EngineeringCli(root).execute(new PrintWriter(out), new PrintWriter(err), args); output = out + err.toString(); return code; } private void ok(String... args) { assertThat(cli(args)).as(output).isZero(); } private void init(int count) throws Exception { ok("init", "--name", "EPIC fixture"); for (int i = 1; i <= count; i++) ok("work", "create", "--mode", "DIRECT", "--approval", "WAIVED", "--human-approved", "--choice-reason", "Explicit synthetic fixture policy", "Work " + i, "--acceptance", "Verified bounded delivery"); Files.writeString(root.resolve("evidence.txt"), "Explicitly synthetic delivery evidence\n"); } private void configure(String id, String mode) { ok("epic", "configure", id, "--mode", mode, "--by", "fixture-agent", "--reason", "Coordinate work"); } private void link(String epic, String child) { ok("epic", "link", epic, child, "--by", "fixture-agent", "--reason", "Decompose scope"); } private void authorize(String epic) { ok("epic", "authorize", epic, "--by", "fixture-owner", "--reason", "The important results satisfy these parent criteria", "--human-approved"); } private Work work(String id) { return new WorkService().load(root, id); } private Path folder(String id) { return new WorkService().folder(root, id); } private void complete(String id) { ok("artifact", "add", id, "--file", "evidence.txt", "--purpose", "Fixture result"); int index = work(id).artifacts().size(); ok("work", "complete", id, "--by", "fixture-owner", "--summary", "Reviewed fixture", "--human-approved", "--criterion", "1=ART-%04d".formatted(index), "--no-integration-needed", "Disposable fixture"); } @Test void allChildrenAreImportantAndAutomaticCompletionHasSystemProvenance() throws Exception { init(3); configure("WRK-0001", "results"); link("WRK-0001", "WRK-0002"); link("WRK-0001", "WRK-0003"); authorize("WRK-0001"); complete("WRK-0002"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.OPEN); assertThat(output).contains("WRK-0003", "epics"); complete("WRK-0003"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); ok("work", "context", "WRK-0001", "--format", "json"); assertThat(output).contains("SYSTEM_EPIC", "\"humanApproved\" : false", "fixture-owner", "WRK-0002", "WRK-0003"); String before = output; ok("work", "context", "WRK-0001", "--format", "json"); assertThat(output).isEqualTo(before); ok("project", "validate"); } @Test void cancelledImportantWorkBlocksUntilExplicitPolicyReviewAndOptionalWorkMayStayOpen() throws Exception { init(4); configure("WRK-0001", "results"); for (String child : List.of("WRK-0002", "WRK-0003", "WRK-0004")) link("WRK-0001", child); ok("epic", "importance", "WRK-0001", "WRK-0004", "--optional", "--by", "fixture-owner", "--reason", "Optional follow-up"); authorize("WRK-0001"); complete("WRK-0002"); ok("work", "status", "WRK-0003", "CANCELLED", "--by", "fixture-owner", "--reason", "Cannot deliver this scope"); assertThat(cli("epic", "check", "WRK-0001", "--format", "json")).isEqualTo(4); assertThat(output).contains("WRK-0003", "CANCELLED"); ok("epic", "importance", "WRK-0001", "WRK-0003", "--optional", "--by", "fixture-owner", "--reason", "Reviewed reduced scope"); assertThat(work("WRK-0001").status()).isNotEqualTo(Work.Status.COMPLETED); authorize("WRK-0001"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); assertThat(work("WRK-0004").status()).isEqualTo(Work.Status.OPEN); ok("project", "validate"); } @Test void decompositionCanCloseWithChildrenStillOpenButNeverClosesAutomatically() throws Exception { init(2); configure("WRK-0001", "decomposition"); link("WRK-0001", "WRK-0002"); ok("epic", "reconcile", "WRK-0001"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.OPEN); complete("WRK-0001"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); assertThat(work("WRK-0002").status()).isEqualTo(Work.Status.OPEN); ok("project", "validate"); } @Test void reopeningImportantDescendantReopensAncestorsAndRetainsCompletionHistory() throws Exception { init(3); configure("WRK-0001", "results"); configure("WRK-0002", "results"); link("WRK-0002", "WRK-0003"); link("WRK-0001", "WRK-0002"); authorize("WRK-0001"); authorize("WRK-0002"); complete("WRK-0003"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); ok("work", "archive", "WRK-0001", "--by", "fixture-owner", "--reason", "Retain delivery"); ok("work", "reopen", "WRK-0003", "--by", "fixture-owner", "--reason", "New observation"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.ACTIVE); assertThat(work("WRK-0001").archived()).isFalse(); assertThat(work("WRK-0002").status()).isEqualTo(Work.Status.ACTIVE); assertThat(work("WRK-0001").completions()).hasSize(1); complete("WRK-0003"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); assertThat(work("WRK-0001").completions()).hasSize(2); ok("project", "validate"); } @Test void cyclesMultipleParentsMissingChildrenAndUnapprovedPolicyAreRejected() throws Exception { init(3); configure("WRK-0001", "results"); configure("WRK-0002", "results"); link("WRK-0001", "WRK-0002"); byte[] before = Files.readAllBytes(folder("WRK-0002").resolve("epic/WRK-0002.md")); assertThat(cli("epic", "link", "WRK-0002", "WRK-0001", "--by", "agent", "--reason", "Cycle")).isIn(2, 4); assertThat(Files.readAllBytes(folder("WRK-0002").resolve("epic/WRK-0002.md"))).isEqualTo(before); assertThat(cli("epic", "link", "WRK-0001", "WRK-9999", "--by", "agent", "--reason", "Missing")).isEqualTo(3); configure("WRK-0003", "results"); assertThat(cli("epic", "link", "WRK-0003", "WRK-0002", "--by", "agent", "--reason", "Two parents")).isIn(2, 4); assertThat(cli("epic", "authorize", "WRK-0001", "--by", "agent", "--reason", "No approval")).isEqualTo(2); ok("project", "validate"); } @Test void reconciliationRecoversMissedPropagationAndRepeatedNotificationDoesNotRewrite() throws Exception { init(2); configure("WRK-0001", "results"); link("WRK-0001", "WRK-0002"); authorize("WRK-0001"); Path parent = folder("WRK-0001").resolve("work.md"); byte[] before = Files.readAllBytes(parent); complete("WRK-0002"); // Reconstruct the precise crash boundary: child committed, parent not yet written. Path evidence = folder("WRK-0001").resolve("artifacts/evidence"); try (var paths = Files.walk(evidence)) { for (Path p : paths.sorted(Comparator.reverseOrder()).filter(p -> !p.equals(evidence)).toList()) Files.delete(p); } Files.write(parent, before); ok("next-actions", "WRK-0001", "--format", "json"); assertThat(output).contains("RECONCILE_EPIC"); ok("epic", "reconcile", "WRK-0001", "--requester", "resuming-agent", "--request-id", "resume-one"); assertThat(work("WRK-0001").status()).isEqualTo(Work.Status.COMPLETED); byte[] completed = Files.readAllBytes(parent); ok("epic", "reconcile", "WRK-0001", "--requester", "resuming-agent", "--request-id", "resume-one"); assertThat(Files.readAllBytes(parent)).isEqualTo(completed); assertThat(work("WRK-0001").completions()).hasSize(1); ok("project", "validate"); } }