Add kernel test for StreamableGuardrailInterface mid-stream buffering and evaluation
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3584951. --> Reported by: [abhisekmazumdar](https://www.drupal.org/user/3557964) >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>[One-line issue summary for stakeholders]<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> <em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <h2>Problem/Motivation</h2> <p><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-13"><a href="https://www.drupal.org/project/ai/issues/3582179" title="Status: Needs work">#3582179: Add streaming-aware guardrails that can buffer and evaluate content in real-time using start/stop regex patterns</a></span> introduced <code>StreamableGuardrailInterface</code> &mdash; a new guardrail capability that hooks into the stream iterator in real time, buffers content between configurable start/stop regex patterns, and evaluates it before it reaches the consumer.</p> <p>The unit tests in <code>tests/src/Unit/OperationType/Chat/StreamingGuardrailTest.php</code> cover the core buffering logic using a mock iterator. However, there is no kernel test that exercises the full stack: a real guardrail plugin implementing <code>StreamableGuardrailInterface</code>, registered on a guardrail set, attached to a <code>ChatInput</code>, flowing through <code>GuardrailsEventSubscriber</code>, and evaluated mid-stream via a real <code>StreamedChatMessageIterator</code>.</p> <p>A kernel test suggested during review of <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-13"><a href="https://www.drupal.org/project/ai/issues/3582179" title="Status: Needs work">#3582179: Add streaming-aware guardrails that can buffer and evaluate content in real-time using start/stop regex patterns</a></span> was intentionally deferred to keep that issue focused.</p> <h2>Proposed resolution</h2> <p>Add a kernel test class (e.g. <code>StreamableGuardrailKernelTest</code>) in <code>tests/src/Kernel/</code> that:</p> <ol> <li>Defines a minimal test guardrail plugin implementing <code>StreamableGuardrailInterface</code> with known start/stop patterns.</li> <li>Creates a guardrail set entity with the test plugin on the post-generate list.</li> <li>Attaches the guardrail set to a <code>ChatInput</code> via <code>AiGuardrailHelper</code>.</li> <li>Uses a mock or virtual provider that returns a predictable streamed response containing both safe content and content matching the start/stop patterns.</li> <li>Asserts that: <ul> <li>Content before the start pattern streams through unchanged.</li> <li>Content between start and stop patterns is suppressed or replaced by the guardrail result.</li> <li>The end-of-stream force-flush fires correctly when the stop pattern never matches.</li> <li>The max guardrail buffer size force-evaluation triggers when the buffer exceeds the limit.</li> </ul> </li> </ol> <h2>Remaining tasks</h2> <ul> <li>Write the kernel test class.</li> <li>Ensure the virtual/mock provider supports streaming in the kernel test environment.</li> </ul> <h3 id="summary-ai-usage">AI usage (if applicable)</h3> <p>[ x ] AI Assisted Issue<br> This issue was generated with AI assistance, but was reviewed and refined by the creator.</p> <p>[ ] AI Assisted Code<br> This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p> <p>[ ] AI Generated Code<br> This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p> <p>[ ] Vibe Coded<br> This code was generated by an AI and has only been functionally tested.</p> > Related issue: [Issue #3582179](https://www.drupal.org/node/3582179)
issue