Resolve #3488376 "Create unit tests"
Closes(hopefully) #3488376
PromptJsonDecoderTest
Explanation:
-
Setup: The
setUp
method initializes thePromptJsonDecoder
service from the container. -
Tests:
-
testDecodeNormalChatMessage
: Tests decoding a normalChatMessage
. -
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:
-
Service Injection: The service under test (
PromptCodeBlockExtractor
) is injected in thesetUp
method. -
Data Providers: Multiple data providers are created to supply different scenarios for testing:
-
payloadDataProvider
: Tests theextractPayload
method with various inputs and expected results. -
chatMessageDataProvider
: Tests theextract
method with aChatMessage
. -
stringDataProvider
: Tests theextract
method with a plain string. -
streamingDataProvider
: Tests theextract
method with aReplayedChatMessageIterator
.
-
-
The
payloadDataProvider
method includes examples for HTML, YAML, JSON, and CSS code blocks. -
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.
-
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.
-
Assertions: The assertions are made using
assertEquals
,assertInstanceOf
, andassertNull
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.