ぼざクリタグ広場 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.
 
 
 
 
 
 

94 lines
3.5 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.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
  13. end
  14. it "既存 post を見つけて、nico tag と linked tag を追加し、差分が出たら bot を付ける" do
  15. # 既存 post(正規表現で拾われるURL)
  16. post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
  17. # 既存の非nicoタグ(kept_non_nico_ids)
  18. kept_general = create_tag!("spec_kept", category: "general")
  19. PostTag.create!(post: post, tag: kept_general)
  20. # 追加される linked tag を準備(nico tag に紐付く一般タグ)
  21. linked = create_tag!("spec_linked", category: "general")
  22. nico = create_tag!("nico:AAA", category: "nico")
  23. link_nico_to_tag!(nico, linked)
  24. # bot / tagme は task 内で使うので作っておく(Tag.bot/tagme がある前提)
  25. Tag.bot
  26. Tag.tagme
  27. # pythonの出力(AAA が付く)
  28. stub_python([{
  29. 'code' => 'sm9',
  30. 'title' => 't',
  31. 'tags' => ['AAA'],
  32. 'uploaded_at' => '2026-01-01 12:34:56',
  33. 'deleted_at' => '2026-01-31 00:00:00' }])
  34. # 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ
  35. allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
  36. run_rake_task("nico:sync")
  37. post.reload
  38. active_tag_names = post.tags.joins(:tag_name).pluck("tag_names.name")
  39. expect(active_tag_names).to include("spec_kept")
  40. expect(active_tag_names).to include("nico:AAA")
  41. expect(active_tag_names).to include("spec_linked")
  42. expect(post.original_created_from).to eq(Time.iso8601('2026-01-01T03:34:00Z'))
  43. expect(post.original_created_before).to eq(Time.iso8601('2026-01-01T03:35:00Z'))
  44. # 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids)
  45. expect(active_tag_names).to include("bot操作")
  46. end
  47. it "既存 post にあった古い nico tag は active から外され、履歴として discard される" do
  48. post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
  49. # 旧nicoタグ(今回の同期結果に含まれない)
  50. old_nico = create_tag!("nico:OLD", category: "nico")
  51. old_pt = PostTag.create!(post: post, tag: old_nico)
  52. expect(old_pt.discarded_at).to be_nil
  53. # 今回は NEW のみ欲しい
  54. new_nico = create_tag!("nico:NEW", category: "nico")
  55. # bot/tagme 念のため
  56. Tag.bot
  57. Tag.tagme
  58. stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["NEW"] }])
  59. allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
  60. run_rake_task("nico:sync")
  61. # OLD は active から外れる(discarded_at が入る)
  62. old_pts = PostTag.where(post_id: post.id, tag_id: old_nico.id).order(:id).to_a
  63. expect(old_pts.last.discarded_at).to be_present
  64. # NEW は active にいる
  65. post.reload
  66. active_names = post.tags.joins(:tag_name).pluck("tag_names.name")
  67. expect(active_names).to include("nico:NEW")
  68. expect(active_names).not_to include("nico:OLD")
  69. end
  70. end