|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Gili Tzabari. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the CAT Commercial License. |
| 5 | + * See LICENSE.md in the project root for license terms. |
| 6 | + */ |
| 7 | +package io.github.cowwoc.cat.hooks.test; |
| 8 | + |
| 9 | +import io.github.cowwoc.cat.hooks.session.InjectMainAgentRules; |
| 10 | +import io.github.cowwoc.cat.hooks.session.SessionStartHandler; |
| 11 | +import org.testng.annotations.Test; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +import static io.github.cowwoc.requirements13.java.DefaultJavaValidators.requireThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * Agent-compliance tests for the tee-piped-output rule. |
| 21 | + * |
| 22 | + * <p>Verifies that the rule in plugin/rules/tee-piped-output.md is properly loaded and injected |
| 23 | + * into the main agent context with correct frontmatter and content. |
| 24 | + */ |
| 25 | +public final class TeePipedOutputRuleLoadTest |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Verifies that the tee-piped-output rule is loaded and injected into the main agent context. |
| 29 | + * |
| 30 | + * <p>The rule must be present in plugin/rules/tee-piped-output.md with mainAgent: true |
| 31 | + * frontmatter. |
| 32 | + * |
| 33 | + * @throws IOException if file operations fail |
| 34 | + */ |
| 35 | + @Test |
| 36 | + public void teePipedOutputRuleLoadedInMainAgent() throws IOException |
| 37 | + { |
| 38 | + Path projectPath = Files.createTempDirectory("tee-rule-main-project-"); |
| 39 | + Path pluginRoot = Files.createTempDirectory("tee-rule-main-plugin-"); |
| 40 | + try (TestClaudeHook scope = new TestClaudeHook(projectPath, pluginRoot, projectPath)) |
| 41 | + { |
| 42 | + Path rulesDir = scope.getPluginRoot().resolve("rules"); |
| 43 | + Files.createDirectories(rulesDir); |
| 44 | + |
| 45 | + Files.writeString(rulesDir.resolve("tee-piped-output.md"), """ |
| 46 | + --- |
| 47 | + mainAgent: true |
| 48 | + --- |
| 49 | + ## Tee Piped Process Output |
| 50 | +
|
| 51 | + **MANDATORY:** When running a Bash command that contains a pipe (`|`), insert `tee` to capture the full output. |
| 52 | + """); |
| 53 | + |
| 54 | + InjectMainAgentRules handler = new InjectMainAgentRules(); |
| 55 | + SessionStartHandler.Result result = handler.handle(scope); |
| 56 | + |
| 57 | + requireThat(result.additionalContext(), "additionalContext"). |
| 58 | + contains("Tee Piped Process Output"); |
| 59 | + requireThat(result.additionalContext(), "additionalContext"). |
| 60 | + contains("insert `tee` to capture the full output"); |
| 61 | + requireThat(result.stderr(), "stderr").isEmpty(); |
| 62 | + } |
| 63 | + finally |
| 64 | + { |
| 65 | + TestUtils.deleteDirectoryRecursively(projectPath); |
| 66 | + TestUtils.deleteDirectoryRecursively(pluginRoot); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + /** |
| 72 | + * Verifies that the tee-piped-output rule includes the complete mktemp and cleanup pattern. |
| 73 | + * |
| 74 | + * <p>The rule must include: create temp file with mktemp, use tee to capture output, and |
| 75 | + * cleanup with rm -f. |
| 76 | + * |
| 77 | + * @throws IOException if file operations fail |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void teePipedOutputRuleIncludesCompletePattern() throws IOException |
| 81 | + { |
| 82 | + Path projectPath = Files.createTempDirectory("tee-pattern-project-"); |
| 83 | + Path pluginRoot = Files.createTempDirectory("tee-pattern-plugin-"); |
| 84 | + try (TestClaudeHook scope = new TestClaudeHook(projectPath, pluginRoot, projectPath)) |
| 85 | + { |
| 86 | + Path rulesDir = scope.getPluginRoot().resolve("rules"); |
| 87 | + Files.createDirectories(rulesDir); |
| 88 | + |
| 89 | + Files.writeString(rulesDir.resolve("tee-piped-output.md"), """ |
| 90 | + --- |
| 91 | + mainAgent: true |
| 92 | + --- |
| 93 | + ## Tee Piped Process Output |
| 94 | +
|
| 95 | + **Pattern:** |
| 96 | +
|
| 97 | + ```bash |
| 98 | + # 1. Create a temporary log file |
| 99 | + LOG_FILE=$(mktemp /tmp/cmd-output-XXXXXX.log) |
| 100 | +
|
| 101 | + # 2. Capture full output before the pipe |
| 102 | + some-command 2>&1 | tee "$LOG_FILE" | grep "pattern" |
| 103 | +
|
| 104 | + # 3. Later, re-filter without re-running the command |
| 105 | + grep -i "error" "$LOG_FILE" |
| 106 | + tail -50 "$LOG_FILE" |
| 107 | +
|
| 108 | + # Cleanup |
| 109 | + rm -f "$LOG_FILE" |
| 110 | + ``` |
| 111 | + """); |
| 112 | + |
| 113 | + InjectMainAgentRules handler = new InjectMainAgentRules(); |
| 114 | + SessionStartHandler.Result result = handler.handle(scope); |
| 115 | + |
| 116 | + String context = result.additionalContext(); |
| 117 | + requireThat(context, "context").contains("mktemp /tmp/cmd-output-XXXXXX.log"); |
| 118 | + requireThat(context, "context").contains("some-command 2>&1 | tee"); |
| 119 | + requireThat(context, "context").contains("$LOG_FILE"); |
| 120 | + requireThat(context, "context").contains("rm -f"); |
| 121 | + requireThat(context, "context").contains("Cleanup"); |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + TestUtils.deleteDirectoryRecursively(projectPath); |
| 126 | + TestUtils.deleteDirectoryRecursively(pluginRoot); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments