Skip to content

Resolve #3488376 "Create unit tests"

spec0 requested to merge issue/ai-3488376:3488376-create-unit-tests into 1.0.x

Closes(hopefully) #3488376

PromptJsonDecoderTest

Explanation:

  1. Setup: The setUp method initializes the PromptJsonDecoder service from the container.
  2. Tests:
    • testDecodeNormalChatMessage: Tests decoding a normal ChatMessage.
    • testDecodeStreamingMessageWithValidJson: Tests decoding a streaming message that contains valid JSON.
    • testDecodeStreamingMessageWithoutValidJson: Tests decoding a streaming message that does not contain valid JSON.
    • testDecodePayloadWithNoJson: Tests decoding a payload that contains no JSON.

PromptCodeBlockExtractorTest

Explanation:

  1. Service Injection: The service under test (PromptCodeBlockExtractor) is injected in the setUp method.

  2. Data Providers: Multiple data providers are created to supply different scenarios for testing:

    • payloadDataProvider: Tests the extractPayload method with various inputs and expected results.
    • chatMessageDataProvider: Tests the extract method with a ChatMessage.
    • stringDataProvider: Tests the extract method with a plain string.
    • streamingDataProvider: Tests the extract method with a ReplayedChatMessageIterator.
  3. The payloadDataProvider method includes examples for HTML, YAML, JSON, and CSS code blocks.

  4. The streamingDataProvider method now returns the first message from the stream so it can be checked in the test.

These modifications ensure that our PromptCodeBlockExtractor service can correctly extract and handle code blocks of different types.

  1. Test Methods: Each test method uses the data providers to feed different inputs into the service and asserts that the output matches the expected result.

  2. Assertions: The assertions are made using assertEquals, assertInstanceOf, and assertNull to validate the correctness of the service's behavior.

Note: These tests are AI generated(for the most part). PromptJsonDecoderTest logic is entirely AI generated while for the PromptCodeBlockExtractorTest I had to manually fix the data providers. There is a deprecation notice on the latter, however I think the problem is in the class we test. A few issues there:

  • The regex in the rules should be /```html\n(.*)\n```/s instead of /```html\n(.*)```/s because the code blocks returned from LLMS are like:
<div>Hello World</div>
  • Also the extractPayload method from line 139 to line 156 is never called in any case
  • Regex rules /<html>(.*)<\/html>/s and /<.*?>(.*)<\/.*?>/s are interchangeable.

Let me know if we need to fix these or this is already planned. I can add to this MR.

Merge request reports

Loading