Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
8.5 KB
Raw Blame
require "test_helper"

class ChatsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:family_admin)
    @family = families(:dylan_family)
    sign_in @user
  end

  test "gets index" do
    Usage.stubs(:capture)
    Usage.expects(:capture).with("ai_assistant_served_daily").never
    get chats_url
    assert_response :success
  end

  test "gets new chat with a localized German default title" do
    @user.update!(locale: "de")

    travel_to Time.zone.local(2026, 8, 29, 12, 34) do
      get new_chat_url

      assert_response :success
      assert_select "turbo-frame#title_chat h3", text: "Neuer Chat 2026-08-29 12:34"
    end
  end

  test "gets new chat with the existing English default title" do
    @user.update!(locale: "en")

    travel_to Time.zone.local(2026, 8, 29, 12, 34) do
      get new_chat_url

      assert_response :success
      assert_select "turbo-frame#title_chat h3", text: "New chat 2026-08-29 12:34"
    end
  end

  test "new chat prefills the Dig in with AI message for one of the family's insights" do
    insight = insights(:spending_anomaly_dining)

    assert_no_difference("Chat.count") do
      get new_chat_url(insight_id: insight.id)
    end

    assert_response :success
    assert_select "textarea[name='chat[content]'][autofocus]", text: /#{Regexp.escape(insight.title)}/
  end

  test "Dig in with AI asks a user with AI off to turn it on before anything is sent" do
    @user.update!(ai_enabled: false)

    get new_chat_url(insight_id: insights(:spending_anomaly_dining).id)

    assert_select "h3", text: I18n.t("chats.ai_consent.title")
    assert_select "textarea[name='chat[content]']", count: 0
  end

  test "new chat ignores another family's insight" do
    insight = insights(:spending_anomaly_dining)
    insight.update_columns(family_id: families(:empty).id)

    get new_chat_url(insight_id: insight.id)

    assert_response :success
    assert_select "textarea[name='chat[content]']", text: ""
  end

  # The feed hides an insight about an account not every member can see, so
  # the chat composer must not hand its text to anyone who has the id.
  test "new chat ignores an insight about an account the member cannot see" do
    insight = create_private_account_insight
    sign_in users(:family_member)

    get new_chat_url(insight_id: insight.id)

    assert_response :success
    assert_select "textarea[name='chat[content]']", text: ""
    assert_no_match "Private holds a balance", response.body
  end

  test "new chat ignores a household total computed from accounts someone can no longer open" do
    insight = families(:dylan_family).insights.create!(
      insight_type: "savings_rate_change", priority: "medium", status: "active", title: "Savings",
      body: "Stale household total", metadata: { "accounts_digest" => "an-account-set-that-changed" },
      dedup_key: "savings_rate_change:stale", generated_at: Time.current
    )

    get new_chat_url(insight_id: insight.id)

    assert_response :success
    assert_select "textarea[name='chat[content]']", text: ""
    assert_no_match "Stale household total", response.body
  end

  # Hidden from everyone, owner included, same as the feed.
  test "new chat ignores an insight about a private account for its owner too" do
    insight = create_private_account_insight

    get new_chat_url(insight_id: insight.id)

    assert_response :success
    assert_select "textarea[name='chat[content]']", text: ""
  end

  test "creates chat" do
    Usage.stubs(:capture)
    Usage.expects(:capture).with("ai_assistant_served_daily").never
    assert_difference("Chat.count") do
      post chats_url, params: { chat: { content: "Hello", ai_model: "gpt-4.1" } }
    end

    chat = Chat.order(created_at: :desc).first

    assert_redirected_to chat_path(chat, thinking: true)
    assert_equal "Hello", chat.title
    assert_equal "Hello", chat.messages.find_by!(type: "UserMessage").content
  end

  test "redirects to chats index instead of creating a chat if AI is disabled" do
    @user.update!(ai_enabled: false)

    assert_no_difference("Chat.count") do
      post chats_url, params: { chat: { content: "Hello", ai_model: "gpt-4.1" } }
    end

    assert_redirected_to chats_path
  end

  test "does not create a chat for a consenting user when the assistant type is mcp_only" do
    @family.update!(assistant_type: "mcp_only")

    assert_no_difference("Chat.count") do
      post chats_url, params: { chat: { content: "Hello", ai_model: "gpt-4.1" } }
    end

    assert_redirected_to chats_path
  end

  test "index shows the AI consent screen instead of the compose form when AI is disabled" do
    @user.update!(ai_enabled: false)
    @user.chats.destroy_all

    get chats_url

    assert_response :success
    assert_select "h3", text: I18n.t("chats.ai_consent.title")
    assert_select "form textarea", count: 0
  end

  test "index shows the AI consent screen next to existing chats when AI is disabled" do
    @user.update!(ai_enabled: false)
    assert @user.chats.any?, "fixture user should have chats so the list branch renders"

    get chats_url

    assert_response :success
    # Scoped to the composer's wrapper: the layout's own consent overlay sits
    # in the desktop-only sidebar, which the mobile Assistant entry never shows.
    assert_select "div.max-w-sm h3", text: I18n.t("chats.ai_consent.title")
    assert_select "form textarea", count: 0
  end

  test "the consent screen tells a family admin, and only an admin, that it also covers the family's background AI" do
    @user.update!(ai_enabled: false)
    get new_chat_url
    assert_includes response.body, I18n.t("chats.ai_consent.family_note")

    sign_in users(:family_member)
    users(:family_member).update!(ai_enabled: false)
    get new_chat_url
    assert_not_includes response.body, I18n.t("chats.ai_consent.family_note")
  end

  test "new shows the AI consent screen instead of the compose form when AI is disabled" do
    @user.update!(ai_enabled: false)

    get new_chat_url

    assert_response :success
    assert_select "h3", text: I18n.t("chats.ai_consent.title")
    assert_select "form textarea", count: 0
  end

  test "show shows the AI consent screen instead of the compose form when AI is disabled" do
    @user.update!(ai_enabled: false)

    get chat_url(chats(:one))

    assert_response :success
    assert_select "h3", text: I18n.t("chats.ai_consent.title")
    assert_select "form textarea", count: 0
  end

  test "shows chat" do
    chat = chats(:one)
    @user.update!(last_viewed_chat: nil)

    get chat_url(chat)

    assert_response :success
    assert_equal chat, @user.reload.last_viewed_chat
  end

  test "destroys chat" do
    assert_difference("Chat.count", -1) do
      delete chat_url(chats(:one))
    end

    assert_redirected_to chats_url
  end

  test "retries last user message" do
    last_viewed_chat = @user.chats.create!(title: "Last Viewed Chat")
    @user.update!(last_viewed_chat: last_viewed_chat)

    chat = chats(:one)
    chat.messages.destroy_all
    # UserMessage's own after_create_commit already calls ask_assistant_later
    # once (creating the pending AssistantMessage reply); clear that out so
    # the user message is the last one left for retry to act on.
    user_message = chat.messages.create!(type: "UserMessage", content: "Hello", ai_model: "gpt-4.1")
    chat.messages.where.not(id: user_message.id).destroy_all

    Chat.any_instance.expects(:ask_assistant_later).with(user_message).once

    post retry_chat_url(chat)

    assert_redirected_to chat_path(chat)
  end

  test "redirects to the chat instead of retrying if AI is disabled" do
    @user.update!(ai_enabled: false)
    chat = chats(:one)

    Chat.any_instance.expects(:ask_assistant_later).never

    post retry_chat_url(chat)

    assert_redirected_to chat_path(chat)
  end

  test "should not allow access to other user's chats" do
    other_user = users(:family_member)
    other_chat = Chat.create!(user: other_user, title: "Other User's Chat")

    get chat_url(other_chat)
    assert_response :not_found

    delete chat_url(other_chat)
    assert_response :not_found

    post retry_chat_url(other_chat)
    assert_response :not_found
  end

  private
    # accounts(:connected) belongs to family_admin and is not shared.
    def create_private_account_insight
      @family.insights.create!(
        insight_type: "idle_cash", priority: "low", status: "active", title: "Idle cash in Private",
        body: "Private holds a balance with no activity in the last 60 days.",
        metadata: { "account_id" => accounts(:connected).id },
        dedup_key: "idle_cash:private:2026-07", generated_at: Time.current
      )
    end
end