#!/usr/bin/env bats
# 単体: name.sh の導出と name_locked 判定（T018, FR-040/044）。
# 実行: bats cc-tmux/tests/unit/test_name.bats

load '../test_helper'

setup() {
  . "$CC_TMUX_LIB/name.sh"
  NTMP="$(mktemp -d "${TMPDIR:-/tmp}/cc-name.XXXXXX")"
}

teardown() {
  rm -rf "$NTMP"
}

@test "transcript（文字列 content）から最初のプロンプトを要約" {
  printf '%s\n' '{"type":"user","message":{"content":"赤いタブ色の機能を作る"}}' > "$NTMP/t.jsonl"
  run name_derive s1 /tmp/x "$NTMP/t.jsonl"
  [ "$status" -eq 0 ]
  [ "$output" = "赤いタブ色の機能を作る" ]
}

@test "transcript（配列 content）から text を抽出" {
  printf '%s\n' '{"type":"user","message":{"content":[{"type":"text","text":"Add OAuth2"}]}}' > "$NTMP/t.jsonl"
  run name_derive s2 /tmp/x "$NTMP/t.jsonl"
  [ "$status" -eq 0 ]
  [ "$output" = "Add OAuth2" ]
}

@test "transcript 無しなら cwd ベース名へフォールバック" {
  run name_derive s3 /Users/foo/ws/myrepo ""
  [ "$status" -eq 0 ]
  [ "$output" = "myrepo" ]
}

@test "cwd も transcript も無ければ失敗（空名にしない）" {
  run name_derive s4 "" ""
  [ "$status" -ne 0 ]
}

@test "長いプロンプトは 24 文字に切り詰める" {
  long=$(printf 'x%.0s' $(seq 1 100))
  printf '%s\n' "{\"type\":\"user\",\"message\":{\"content\":\"$long\"}}" > "$NTMP/t.jsonl"
  run name_derive s5 /tmp/x "$NTMP/t.jsonl"
  [ "$status" -eq 0 ]
  [ "${#output}" -le 24 ]
}

@test "name_follow は title/summary を読む" {
  printf '%s\n' '{"type":"summary","title":"renamed-session"}' > "$NTMP/t.jsonl"
  run name_follow s6 "$NTMP/t.jsonl"
  [ "$status" -eq 0 ]
  [ "$output" = "renamed-session" ]
}

@test "name_follow は title 無しなら失敗（静かに諦める）" {
  printf '%s\n' '{"type":"user","message":{"content":"hi"}}' > "$NTMP/t.jsonl"
  run name_follow s7 "$NTMP/t.jsonl"
  [ "$status" -ne 0 ]
}
