if(NOT DEFINED HZ OR NOT DEFINED FIXTURE_DIR OR NOT DEFINED REQUEST_FILE OR NOT DEFINED OUTPUT)
    message(FATAL_ERROR "HZ, FIXTURE_DIR, REQUEST_FILE, and OUTPUT are required")
endif()

file(READ "${FIXTURE_DIR}/main.hz" source)
set(text "${source}fn probe() { seq:: }\n")
string(REPLACE "\\" "\\\\" text_json "${text}")
string(REPLACE "\"" "\\\"" text_json "${text_json}")
string(REPLACE "\n" "\\n" text_json "${text_json}")
string(REPLACE "\r" "\\r" text_json "${text_json}")

file(TO_CMAKE_PATH "${FIXTURE_DIR}/main.hz" main_path)
set(main_uri "file://${main_path}")

set(messages "")

macro(add_frame json)
    string(LENGTH "${json}" length)
    string(APPEND messages "Content-Length: ${length}\r\n\r\n${json}")
endmacro()

add_frame("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"rootUri\":\"file://${FIXTURE_DIR}\",\"capabilities\":{}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}")
add_frame("{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didOpen\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\",\"languageId\":\"haze\",\"version\":1,\"text\":\"${text_json}\"}}}")

add_frame("{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"textDocument/completion\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":9,\"character\":18}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"textDocument/completion\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":2,\"character\":27}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":4,\"method\":\"textDocument/signatureHelp\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":6,\"character\":19}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":5,\"method\":\"textDocument/signatureHelp\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":7,\"character\":17}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":6,\"method\":\"textDocument/hover\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":7,\"character\":12}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"textDocument/definition\",\"params\":{\"textDocument\":{\"uri\":\"${main_uri}\"},\"position\":{\"line\":6,\"character\":15}}}")
add_frame("{\"jsonrpc\":\"2.0\",\"id\":8,\"method\":\"shutdown\",\"params\":null}")
add_frame("{\"jsonrpc\":\"2.0\",\"method\":\"exit\",\"params\":null}")

file(WRITE "${REQUEST_FILE}" "${messages}")

execute_process(
    COMMAND "${HZ}" lsp
    INPUT_FILE "${REQUEST_FILE}"
    OUTPUT_FILE "${OUTPUT}"
    RESULT_VARIABLE result
    ERROR_VARIABLE error
    TIMEOUT 20)
if(NOT result EQUAL 0)
    message(FATAL_ERROR "hz lsp failed: ${error}")
endif()

file(READ "${OUTPUT}" response)

foreach(needle
        "\"completionProvider\""
        "\"signatureHelpProvider\""
        "\"hoverProvider\":true"
        "\"definitionProvider\":true"
        "\"detail\":\"fn range(a i32, b i32) list[i32]\""
        "\"label\":\"left\""
        "\"label\":\"right\""
        "\"signatures\""
        "\"activeParameter\":0"
        "fn helper(p Pair) i32"
        "seq.hz")
    string(FIND "${response}" "${needle}" position)
    if(position EQUAL -1)
        message(FATAL_ERROR "LSP response did not contain ${needle}\n${response}")
    endif()
endforeach()
