ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
3.3 KiB

  1. require "rails_helper"
  2. RSpec.describe "nico:sync" do
  3. def stub_python(json_array)
  4. status = instance_double(Process::Status, success?: true)
  5. allow(Open3).to receive(:capture3).and_return([json_array.to_json, "", status])
  6. end
  7. def create_tag!(name, category:)
  8. tn = TagName.find_or_create_by!(name: name.to_s.strip)
  9. Tag.find_or_create_by!(tag_name_id: tn.id) { |t| t.category = category }
  10. end
  11. def link_nico_to_tag!(nico_tag, tag)
  12. # NicoTagRelation がある前提(君の model からそう見える)
  13. NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
  14. end
  15. it "既存 post を見つけて、nico tag と linked tag を追加し、差分が出たら bot を付ける" do
  16. # 既存 post(正規表現で拾われるURL)
  17. post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
  18. # 既存の非nicoタグ(kept_non_nico_ids)
  19. kept_general = create_tag!("spec_kept", category: "general")
  20. PostTag.create!(post: post, tag: kept_general)
  21. # 追加される linked tag を準備(nico tag に紐付く一般タグ)
  22. linked = create_tag!("spec_linked", category: "general")
  23. nico = create_tag!("nico:AAA", category: "nico")
  24. link_nico_to_tag!(nico, linked)
  25. # bot / tagme は task 内で使うので作っておく(Tag.bot/tagme がある前提)
  26. Tag.bot
  27. Tag.tagme
  28. # pythonの出力(AAA が付く)
  29. stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["AAA"] }])
  30. # 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ
  31. allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
  32. run_rake_task("nico:sync")
  33. post.reload
  34. active_tag_names = post.tags.joins(:tag_name).pluck("tag_names.name")
  35. expect(active_tag_names).to include("spec_kept")
  36. expect(active_tag_names).to include("nico:AAA")
  37. expect(active_tag_names).to include("spec_linked")
  38. # 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids)
  39. expect(active_tag_names).to include("bot操作")
  40. end
  41. it "既存 post にあった古い nico tag は active から外され、履歴として discard される" do
  42. post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
  43. # 旧nicoタグ(今回の同期結果に含まれない)
  44. old_nico = create_tag!("nico:OLD", category: "nico")
  45. old_pt = PostTag.create!(post: post, tag: old_nico)
  46. expect(old_pt.discarded_at).to be_nil
  47. # 今回は NEW のみ欲しい
  48. new_nico = create_tag!("nico:NEW", category: "nico")
  49. # bot/tagme 念のため
  50. Tag.bot
  51. Tag.tagme
  52. stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["NEW"] }])
  53. allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
  54. run_rake_task("nico:sync")
  55. # OLD は active から外れる(discarded_at が入る)
  56. old_pts = PostTag.where(post_id: post.id, tag_id: old_nico.id).order(:id).to_a
  57. expect(old_pts.last.discarded_at).to be_present
  58. # NEW は active にいる
  59. post.reload
  60. active_names = post.tags.joins(:tag_name).pluck("tag_names.name")
  61. expect(active_names).to include("nico:NEW")
  62. expect(active_names).not_to include("nico:OLD")
  63. end
  64. end