require 'rails_helper'


RSpec.describe 'tag_similarity:calc' do
  include RakeTaskHelper

  it 'calls Similarity::Calc with Tag and :posts' do
    # 必要最低限のデータ
    t1 = Tag.create!(name: "t1")
    t2 = Tag.create!(name: "t2")
    t3 = Tag.create!(name: "t3")

    p1 = Post.create!(url: "https://example.com/1")
    p2 = Post.create!(url: "https://example.com/2")
    p3 = Post.create!(url: "https://example.com/3")

    # kept スコープが絡むなら、PostTag がデフォで kept になる前提
    PostTag.create!(post: p1, tag: t1)
    PostTag.create!(post: p1, tag: t2)

    PostTag.create!(post: p2, tag: t1)
    PostTag.create!(post: p2, tag: t3)

    PostTag.create!(post: p3, tag: t3)

    expect { run_rake_task("tag_similarity:calc") }
      .to change { TagSimilarity.count }.from(0)

    ps = TagSimilarity.find_by!(tag_id: t1.id, target_tag_id: t2.id)
    ps_rev = TagSimilarity.find_by!(tag_id: t2.id, target_tag_id: t1.id)
    expect(ps_rev.cos).to eq(ps.cos)
  end
end

