From 888842451d172d83158c139b894ed67fa95e4cdb Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 28 Jan 2026 01:51:55 +0900 Subject: [PATCH] =?UTF-8?q?#20=20=E3=83=86=E3=82=B9=E3=83=88=E3=83=BB?= =?UTF-8?q?=E3=82=B1=E3=83=BC=E3=82=B9=E3=81=AE=E3=81=BF=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/spec/requests/posts_spec.rb | 40 +++++++++++++++++++++++++++++ backend/spec/requests/tags_spec.rb | 15 +++++++++++ 2 files changed, 55 insertions(+) diff --git a/backend/spec/requests/posts_spec.rb b/backend/spec/requests/posts_spec.rb index 07523b1..24e790a 100644 --- a/backend/spec/requests/posts_spec.rb +++ b/backend/spec/requests/posts_spec.rb @@ -30,6 +30,7 @@ RSpec.describe 'Posts API', type: :request do let!(:tag) { Tag.create!(tag_name:, category: "general") } let!(:tag_name2) { TagName.create!(name: 'unko') } let!(:tag2) { Tag.create!(tag_name: tag_name2, category: 'deerjikist') } + let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) } let!(:hit_post) do Post.create!(uploaded_user: user, title: "hello spec world", @@ -86,6 +87,25 @@ RSpec.describe 'Posts API', type: :request do end end + it "filters posts by q (hit case by alias)" do + get "/posts", params: { tags: "manko" } + + expect(response).to have_http_status(:ok) + posts = json.fetch('posts') + ids = posts.map { |p| p['id'] } + + expect(ids).to include(hit_post.id) + expect(ids).not_to include(miss_post.id) + expect(json['count']).to be_an(Integer) + + posts.each do |p| + expect(p['tags']).to be_an(Array) + p['tags'].each do |t| + expect(t).to include('name', 'category', 'has_wiki') + end + end + end + it "returns empty posts when nothing matches" do get "/posts", params: { tags: "no_such_keyword_12345" } @@ -167,6 +187,26 @@ RSpec.describe 'Posts API', type: :request do expect(json['tags'][0]).to have_key('name') end + it '201 and creates post + tags when member and tags have aliases' do + sign_in_as(member) + + post '/posts', params: { + title: 'new post', + url: 'https://example.com/new', + tags: 'manko', # 既存タグ名を投げる + thumbnail: dummy_upload + } + + expect(response).to have_http_status(:created) + expect(json).to include('id', 'title', 'url') + + # tags が name を含むこと(API 側の serialization が正しいこと) + expect(json).to have_key('tags') + expect(json['tags']).to be_an(Array) + expect(json['tags'][0]).to have_key('name') + expect(json['tags'][0]['name']).to eq('spec_tag') + end + context "when nico tag already exists in tags" do before do Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'), diff --git a/backend/spec/requests/tags_spec.rb b/backend/spec/requests/tags_spec.rb index 11cce46..c9aad4f 100644 --- a/backend/spec/requests/tags_spec.rb +++ b/backend/spec/requests/tags_spec.rb @@ -5,6 +5,7 @@ require 'rails_helper' RSpec.describe 'Tags API', type: :request do let!(:tn) { TagName.create!(name: 'spec_tag') } let!(:tag) { Tag.create!(tag_name: tn, category: 'general') } + let!(:alias_tn) { TagName.create!(name: 'unko', canonical: tn) } describe 'GET /tags' do it 'returns tags with name' do @@ -56,6 +57,20 @@ RSpec.describe 'Tags API', type: :request do expect(json).to be_an(Array) expect(json.map { |t| t['name'] }).to include('spec_tag') + t = json.find { |t| t['name'] == 'spec_tag' } + expect(t).to have_key('matched_alias') + expect(t['matched_alias']).to be(nil) + end + + it 'returns matching canonical tags by q with aliases' do + get '/tags/autocomplete', params: { q: 'unk' } + + expect(response).to have_http_status(:ok) + + expect(json).to be_an(Array) + expect(json.map { |t| t['name'] }).to include('spec_tag') + t = json.find { |t| t['name'] == 'spec_tag' } + expect(t['matched_alias']).to eq('unko') end end