kcode-k1-chat-thread-web-code-open-targets 0.2.0

Pure source presentation and target classification for K1 Chat WebCode
Documentation
use kcode_k1_chat_boxes::{
    BoxId, ChatBox, ToolCallId, ToolMessageMetadata, USER_MESSAGE_TYPE, tool_message_box,
};
use kcode_k1_chat_thread_web_code_open_targets::{
    TargetRejection, classify_original_target, present_create, present_open,
};
use kcode_k1_chat_web_code_output::DocumentPart;
use kcode_k1_transaction_id::TxId;
use kcode_k1_web_code_document::{CodeDocument, Language};
use kcode_k1_web_package::{AuthorityId, WebFamily};

fn call() -> ToolCallId {
    ToolCallId::new([2; 12], 4)
}
fn document() -> CodeDocument {
    CodeDocument::new(
        WebFamily::new(AuthorityId::new(TxId::from_bytes([1; 12])), "alpha").unwrap(),
        b"<!-- k1-web/v1\n{\"dependencies\":[]}\n-->\nDocs\r\n".to_vec(),
        Language::JavaScript,
        format!(
            "export const a = \"{}\";\nexport const b = \"{}\";\n",
            "a".repeat(850),
            "b".repeat(850)
        )
        .into_bytes(),
    )
    .unwrap()
}
fn assign(id: u64, value: ChatBox) -> ChatBox {
    ChatBox::new(
        BoxId::new(id),
        value.box_type().to_owned(),
        value.contents().to_owned(),
        value.hidden_type().to_owned(),
        value.hidden_contents().to_owned(),
    )
}
fn message(
    id: u64,
    layout: kcode_k1_chat_thread_web_code_open_targets::SourceLayout,
    index: u64,
    body: &str,
) -> ChatBox {
    assign(
        id,
        tool_message_box(&ToolMessageMetadata {
            tool_call_id: layout.tool_call_id(),
            originating_call: layout.originating_call(),
            message_index: index,
            message: body.into(),
        })
        .unwrap(),
    )
}

#[test]
fn all_source_is_messages_and_result_is_status() {
    let document = document();
    let opened = present_open(BoxId::new(7), call(), &document, "1.2.3");
    assert_eq!(opened.output.messages.len(), 3);
    assert_eq!(
        opened.output.messages[0].as_bytes(),
        document.documentation()
    );
    let code = opened.output.messages[1..].concat();
    assert_eq!(code.as_bytes(), document.code());
    assert_eq!(opened.output.result, Ok("Opened 1.2.3.".into()));
    assert!(!opened.output.metadata_contents.contains("authority"));
    let created = present_create(BoxId::new(7), call(), &document, "010101010101010101010101");
    assert_eq!(
        created.output.result,
        Ok("Created 010101010101010101010101.".into())
    );
}

#[test]
fn only_original_messages_are_targets() {
    let value = present_open(BoxId::new(7), call(), &document(), "1.2.3");
    let history = vec![
        message(10, value.layout, 1, &value.output.messages[0]),
        message(11, value.layout, 2, &value.output.messages[1]),
        ChatBox::new(
            BoxId::new(12),
            USER_MESSAGE_TYPE.into(),
            "ordinary".into(),
            String::new(),
            String::new(),
        ),
    ];
    assert_eq!(
        classify_original_target(&history, BoxId::new(10), value.layout),
        Ok(DocumentPart::Documentation)
    );
    assert_eq!(
        classify_original_target(&history, BoxId::new(11), value.layout),
        Ok(DocumentPart::Code)
    );
    assert_eq!(
        classify_original_target(&history, BoxId::new(12), value.layout),
        Err(TargetRejection::NotSourceMessage)
    );
    assert_eq!(
        classify_original_target(&history, BoxId::new(99), value.layout),
        Err(TargetRejection::HistoryAbsent)
    );
}