nekolas note

毎日書くことを目指しているけど全然達成できてないブログ

3.2 KB Raw
//! 統合テスト: push(selection_changed)と pull(getCurrentSelection)の整合(T023)。
//! 参照: contracts/zed-ide-bridge.md
//!
//! 実ソケットを使わず、公開 API(SharedState + payload 組立)で契約を検証する。
//! ソケット往復は手動 quickstart シナリオ5で確認する。

use cc_ide_server::notify::selection_changed_payload;
use cc_ide_server::state::{EditorContext, Position, Selection, SharedState};

#[tokio::test]
async fn push_broadcasts_on_change() {
    let state = SharedState::new();
    let mut rx = state.subscribe();

    let ctx = EditorContext {
        file_path: Some("/a/b.rs".into()),
        file_url: Some("file:///a/b.rs".into()),
        text: "bcd".to_string(),
        selection: Some(Selection {
            start: Position {
                line: 1,
                character: 0,
            },
            end: Position {
                line: 2,
                character: 5,
            },
            is_empty: false,
        }),
    };
    state.update(ctx.clone()).await;

    // push が届くこと。
    let got = rx.recv().await.expect("push");
    assert_eq!(got.file_path.as_deref(), Some("/a/b.rs"));

    // 同値 update では再 push されない(無駄打ち防止)。
    state.update(ctx).await;
    // 別の変化を入れて、それが次に来ることで「中間の同値が飛んでいない」ことを確認。
    let ctx2 = EditorContext {
        file_path: Some("/a/c.rs".into()),
        file_url: Some("file:///a/c.rs".into()),
        text: String::new(),
        selection: None,
    };
    state.update(ctx2).await;
    let got2 = rx.recv().await.expect("push2");
    assert_eq!(got2.file_path.as_deref(), Some("/a/c.rs"));
}

#[tokio::test]
async fn pull_snapshot_reflects_latest() {
    let state = SharedState::new();
    let ctx = EditorContext {
        file_path: Some("/x/y.ts".into()),
        file_url: Some("file:///x/y.ts".into()),
        text: String::new(),
        selection: Some(Selection {
            start: Position {
                line: 10,
                character: 2,
            },
            end: Position {
                line: 10,
                character: 2,
            },
            is_empty: true,
        }),
    };
    state.update(ctx).await;

    // pull 相当: snapshot から payload を組み立てる(getCurrentSelection と同じ経路)。
    let snap = state.snapshot().await;
    let payload = selection_changed_payload(&snap);
    assert_eq!(payload["filePath"], "/x/y.ts");
    assert_eq!(payload["selection"]["isEmpty"], true);
    assert_eq!(payload["selection"]["start"]["line"], 10);
}

#[tokio::test]
async fn untitled_is_safe() {
    // filePath 無し(untitled)でも payload 組立がパニックしない(FR-023)。
    let state = SharedState::new();
    let ctx = EditorContext {
        file_path: None,
        file_url: Some("untitled:Untitled-1".into()),
        text: String::new(),
        selection: None,
    };
    state.update(ctx).await;
    let snap = state.snapshot().await;
    let payload = selection_changed_payload(&snap);
    assert_eq!(payload["filePath"], serde_json::Value::Null);
    assert_eq!(payload["selection"]["isEmpty"], true);
}