From 6ca18a1f48dd49e8a40a6decfe4d605e92bfbe99 Mon Sep 17 00:00:00 2001 From: Codex Night Worker Date: Mon, 23 Nov 2026 00:25:10 +0900 Subject: [PATCH] AI: address review on PR #488 --- backend/app/controllers/tags_controller.rb | 2 +- backend/spec/requests/tags_spec.rb | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/backend/app/controllers/tags_controller.rb b/backend/app/controllers/tags_controller.rb index 5730ac4..15ef924 100644 --- a/backend/app/controllers/tags_controller.rb +++ b/backend/app/controllers/tags_controller.rb @@ -219,7 +219,7 @@ class TagsController < ApplicationController locale = resolve_locale! q = params[:q].to_s.strip.sub(/\Anot:/i, '') - prefix = "#{ ActiveRecord::Base.sanitize_sql_like(q) }%" + prefix = "#{ ActiveRecord::Base.sanitize_sql_like(q).tr('*?', '%_') }%" with_nico = bool?(:nico, default: true) present_only = bool?(:present, default: true) diff --git a/backend/spec/requests/tags_spec.rb b/backend/spec/requests/tags_spec.rb index 988c87d..da16111 100644 --- a/backend/spec/requests/tags_spec.rb +++ b/backend/spec/requests/tags_spec.rb @@ -479,6 +479,41 @@ RSpec.describe 'Tags API', type: :request do end describe 'GET /tags/autocomplete' do + context 'when q contains wildcards' do + let(:names) { ['wildend', 'wildXend', 'wildABend', 'otherwildXend'] } + + before do + names.each_with_index do |name, index| + create(:tag, primary_name: name, post_count: 1) + alias_target = create(:tag, primary_name: "target_#{ index }", post_count: 1) + create(:tag_name, :alias, name: "#{ name }_alias", tag: alias_target) + create(:external_tag, name:, post_count: 1) + end + end + + { + 'wild*end' => [0, 1, 2], + 'wild?end' => [1], + 'not:wild?end' => [1], + 'nico:wild?end' => [1] }.each do |query, indices| + it "matches primary, alias, and external names with #{ query.inspect }" do + get '/tags/autocomplete', params: { q: query } + + expected_names = indices.flat_map do |index| + name = names[index] + query.start_with?('nico:') ? ["nico:#{ name }"] : + [name, "target_#{ index }", "nico:#{ name }"] + end + expect(response).to have_http_status(:ok) + expect(json.map { |row| row.fetch('name') }).to match_array(expected_names) + unless query.start_with?('nico:') + row = json.find { |item| item.fetch('name') == "target_#{ indices.first }" } + expect(row.fetch('matched_alias')).to eq("#{ names[indices.first] }_alias") + end + end + end + end + it 'combines internal and external matches without conflating equal ids' do internal = create(:tag, category: :general, primary_name: 'mixed_internal', post_count: 2) external = create(:external_tag, id: internal.id,