@Override
    @Nullable
    public ResultChunk nextChunk() throws IOException {
        if (limitReached()) {
            LOG.debug("[{}] Reached limit for query {}", queryHash, getOriginalQuery());
            return null;
        }

        final R result = this.initialResult != null ? this.initialResult : nextSearchResult();
        this.lastSearchResponse = result;
        this.initialResult = null;

        final List<ResultMessage> resultMessages = result != null ? collectMessagesFromResult(result) : List.of();

        if (resultMessages.isEmpty()) {
            // chunking exhausted
            LOG.debug("[{}] Reached end of {} results for query {}", queryHash, getChunkingMethodName(), getOriginalQuery());
            return null;
        }

        final int remainingResultsForLimit = limit - resultCount;

        final List<ResultMessage> resultMessagesSlice = (limit != -1 && remainingResultsForLimit < resultMessages.size())
                ? resultMessages.subList(0, remainingResultsForLimit)
                : resultMessages;

        resultCount += resultMessagesSlice.size();

        return new ResultChunk(fields, chunkId++, resultMessagesSlice);
    }
@Test
    void getsFirstChunkIfInitialResultIsNull(ResultMessageFactory resultMessageFactory) throws Exception {
        toTest = new ServerlessChunkedQueryResultSimulation(resultMessageFactory,
                "Client",
                null,
                "",
                List.of("name"),
                10,
                2
        );

        final ResultChunk resultChunk = toTest.nextChunk();
        assertThat(resultChunk.isFirstChunk()).isTrue();
        final List<ResultMessage> messages = resultChunk.messages();
        assertThat(messages)
                .isNotNull()
                .hasSize(2);
        verifyElementAt(messages, 0, BACKING_RESULT_LIST.get(0));
        verifyElementAt(messages, 1, BACKING_RESULT_LIST.get(1));
    }