Merge remote-tracking branch 'origin/main' into feature/351

このコミットが含まれているのは:
2026-06-22 12:39:17 +09:00
コミット 469228a6ed
162個のファイルの変更16567行の追加1180行の削除
+4
ファイルの表示
@@ -1,6 +1,10 @@
require 'rails_helper'
RSpec.describe TagNameSanitisationRule, type: :model do
before do
described_class.unscoped.delete_all
end
describe '.sanitise' do
before do
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
+73
ファイルの表示
@@ -1,6 +1,79 @@
require 'rails_helper'
RSpec.describe Tag, type: :model do
describe '.normalise_tags!' do
it 'rejects deprecated tags when deny_deprecated is enabled' do
tag_name = TagName.create!(name: 'normalise deprecated tag')
deprecated_tag = Tag.create!(
tag_name:,
category: :general,
deprecated_at: 1.day.from_now
)
expect {
described_class.normalise_tags!(
[deprecated_tag.name],
deny_deprecated: true
)
}.to raise_error(Tag::DeprecatedTagNormalisationError) { |error|
expect(error.tag_names).to eq([deprecated_tag.name])
}
end
end
describe '.expand_parent_tags' do
it 'expands through multiple deprecated parents to an active ancestor' do
child = create(:tag, name: 'expand_child')
deprecated_parent = create(
:tag,
name: 'expand_deprecated_parent',
deprecated_at: Time.current
)
deprecated_grandparent = create(
:tag,
name: 'expand_deprecated_grandparent',
deprecated_at: Time.current
)
active_ancestor = create(:tag, name: 'expand_active_ancestor')
TagImplication.create!(tag: child, parent_tag: deprecated_parent)
TagImplication.create!(tag: deprecated_parent, parent_tag: deprecated_grandparent)
TagImplication.create!(tag: deprecated_grandparent, parent_tag: active_ancestor)
expanded = described_class.expand_parent_tags([child])
expect(expanded).to include(
child,
deprecated_parent,
deprecated_grandparent,
active_ancestor
)
expect(expanded.reject(&:deprecated?)).to contain_exactly(child, active_ancestor)
end
it 'terminates when implications contain a cycle' do
first = create(:tag, name: 'expand_cycle_first')
second = create(:tag, name: 'expand_cycle_second')
TagImplication.create!(tag: first, parent_tag: second)
TagImplication.create!(tag: second, parent_tag: first)
expect(described_class.expand_parent_tags([first])).to contain_exactly(first, second)
end
end
describe 'deprecated validation' do
it 'rejects deprecated nico tags' do
tag = build(
:tag,
name: 'nico:deprecated_validation',
category: :nico,
deprecated_at: Time.current
)
expect(tag).not_to be_valid
expect(tag.errors[:deprecated_at]).to include('ニコタグは廃止できません.')
end
end
describe '.merge_tags!' do
let!(:target_tag) { create(:tag, category: :general) }
let!(:source_tag) { create(:tag, category: :general) }
+47
ファイルの表示
@@ -0,0 +1,47 @@
require 'rails_helper'
RSpec.describe 'error responses', type: :request do
describe 'manual input errors' do
it 'returns a stable payload for bad requests' do
get '/tags/name/%20/deerjikists'
expect(response).to have_http_status(:bad_request)
expect(json).to include(
'type' => 'bad_request',
'message' => be_present,
'errors' => {},
'base_errors' => [be_present])
end
it 'returns a stable field-error payload for unprocessable requests' do
member = create(:user, :member)
tag = create(:tag, :general, name: 'error_response_tag')
sign_in_as(member)
patch "/tags/#{ tag.id }", params: { category: 'nico' }
expect(response).to have_http_status(:unprocessable_entity)
expect(json).to include(
'type' => 'validation_error',
'message' => '入力内容を確認してください.',
'base_errors' => [])
expect(json.fetch('errors')).to include(
'category' => ['ニコタグは変更できません.'])
end
end
describe 'model validation errors' do
it 'returns field messages for model errors' do
user = create(:user)
sign_in_as(user)
put "/users/#{ user.id }", params: { name: 'a' * 256 }
expect(response).to have_http_status(:unprocessable_entity)
expect(json).to include(
'type' => 'validation_error',
'message' => '入力内容を確認してください.')
expect(json.fetch('errors').fetch('name')).to include(be_present)
end
end
end
+76
ファイルの表示
@@ -0,0 +1,76 @@
require 'rails_helper'
RSpec.describe 'Gekanator games API', type: :request do
let!(:admin) { create_admin_user! }
let!(:user) { create_member_user! }
let!(:guessed_post) { Post.create!(title: 'guess', url: 'https://example.com/guess') }
let!(:correct_post) { Post.create!(title: 'correct', url: 'https://example.com/correct') }
describe 'POST /gekanator/games' do
it 'stores a won game' do
sign_in_as admin
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: guessed_post.id,
answers: [{ question_id: 'tag:1', answer: 'yes' }] }
expect(response).to have_http_status(:created)
game = GekanatorGame.find(json['id'])
expect(game.user).to eq(admin)
expect(game.guessed_post).to eq(guessed_post)
expect(game.correct_post).to eq(guessed_post)
expect(game.won).to eq(true)
expect(game.question_count).to eq(1)
expect(game.answers).to eq([{ 'question_id' => 'tag:1', 'answer' => 'yes' }])
end
it 'stores a lost game with the correct post' do
sign_in_as admin
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: correct_post.id,
question_count: 4,
answers: [{ question_id: 'tag:1', answer: 'no' }] }
expect(response).to have_http_status(:created)
game = GekanatorGame.find(json['id'])
expect(game.correct_post).to eq(correct_post)
expect(game.won).to eq(false)
expect(game.question_count).to eq(1)
end
it 'rejects a game without the correct post' do
sign_in_as admin
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
question_count: 4,
answers: [{ question_id: 'tag:1', answer: 'no' }] }
expect(response).to have_http_status(:unprocessable_entity)
end
it 'returns unauthorized without a user' do
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: guessed_post.id,
answers: [] }
expect(response).to have_http_status(:unauthorized)
end
it 'stores a game for a non-admin user' do
sign_in_as user
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: guessed_post.id,
answers: [{ question_id: 'tag:1', answer: 'yes' }] }
expect(response).to have_http_status(:created)
expect(GekanatorGame.find(json['id']).user).to eq(user)
end
end
end
ファイル差分が大きすぎるため省略します 差分を読込み
+33
ファイルの表示
@@ -0,0 +1,33 @@
require 'rails_helper'
RSpec.describe 'Gekanator posts API', type: :request do
describe 'GET /gekanator/posts' do
it 'omits deprecated tags and returns the stored similarity cosine' do
active_tag = Tag.create!(name: 'active tag', category: :general)
deprecated_tag = Tag.create!(
name: 'deprecated tag',
category: :general,
deprecated_at: Time.current
)
post_record = Post.create!(title: 'source', url: 'https://example.com/source')
target_post = Post.create!(title: 'target', url: 'https://example.com/target')
PostTag.create!(post: post_record, tag: active_tag)
PostTag.create!(post: post_record, tag: deprecated_tag)
PostTag.create!(post: target_post, tag: deprecated_tag)
PostSimilarity.create!(post: post_record, target_post:, cos: 0.375)
get '/gekanator/posts'
expect(response).to have_http_status(:ok)
post_json = json.fetch('posts').find { |post| post.fetch('id') == post_record.id }
expect(post_json.fetch('tags').map { |tag| tag.fetch('name') }).to eq(['active tag'])
expect(post_json.fetch('post_similarity_edges')).to contain_exactly(
'target_post_id' => target_post.id,
'cos' => 0.375
)
end
end
end
+18 -8
ファイルの表示
@@ -141,16 +141,21 @@ RSpec.describe 'Materials API', type: :request do
context 'when logged in' do
before { sign_in_as(guest_user) }
it 'returns 400 when tag is blank' do
it 'returns 422 when tag is blank' do
post '/materials', params: { tag: ' ', file: dummy_upload }
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tag' => ['タグは必須です.'])
end
it 'returns 400 when both file and url are blank' do
it 'returns 422 when both file and url are blank' do
post '/materials', params: { tag: 'material_create_blank' }
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'file' => ['ファイルまたは URL は必須です.'],
'url' => ['ファイルまたは URL は必須です.'])
end
it 'creates a material with an attached file' do
@@ -261,21 +266,26 @@ RSpec.describe 'Materials API', type: :request do
expect(response).to have_http_status(:not_found)
end
it 'returns 400 when tag is blank' do
it 'returns 422 when tag is blank' do
put "/materials/#{ material.id }", params: {
tag: ' ',
file: dummy_upload
}
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tag' => ['タグは必須です.'])
end
it 'returns 400 when both file and url are blank' do
it 'returns 422 when both file and url are blank' do
put "/materials/#{ material.id }", params: {
tag: 'material_update_no_payload'
}
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'file' => ['ファイルまたは URL は必須です.'],
'url' => ['ファイルまたは URL は必須です.'])
end
it 'updates tag, url, file, and updated_by_user' do
+93 -7
ファイルの表示
@@ -3,12 +3,68 @@ require 'rails_helper'
RSpec.describe 'NicoTags', type: :request do
describe 'GET /tags/nico' do
it 'returns tags and next_cursor when overflowing limit' do
create_list(:tag, 21, :nico)
get '/tags/nico', params: { limit: 20 }
it 'returns paginated tags and total count' do
create_list(:tag, 3, :nico)
get '/tags/nico', params: { page: 2, limit: 2 }
expect(response).to have_http_status(:ok)
expect(json['tags'].size).to eq(20)
expect(json['next_cursor']).to be_present
expect(json['tags'].size).to eq(1)
expect(json['count']).to eq(3)
end
it 'filters by nico tag name, linked tag name, and link status' do
linked = create(:tag, :nico)
linked.tag_name.update!(name: 'nico:search_linked')
unlinked = create(:tag, :nico)
unlinked.tag_name.update!(name: 'nico:search_unlinked')
other = create(:tag, :nico)
other.tag_name.update!(name: 'nico:other')
destination = create(:tag, :general)
destination.tag_name.update!(name: 'destination_search')
NicoTagRelation.create!(nico_tag: linked, tag: destination)
NicoTagRelation.create!(nico_tag: other, tag: create(:tag, :general))
get '/tags/nico', params: {
name: 'search_',
linked_tag: 'destination_',
link_status: 'linked'
}
expect(response).to have_http_status(:ok)
expect(json['count']).to eq(1)
expect(json.fetch('tags').map { |tag| tag['id'] }).to eq([linked.id])
get '/tags/nico', params: { name: 'search_', link_status: 'unlinked' }
expect(json['count']).to eq(1)
expect(json.fetch('tags').map { |tag| tag['id'] }).to eq([unlinked.id])
end
it 'sorts by name and timestamps' do
older = create(:tag, :nico)
older.tag_name.update!(name: 'nico:a')
older.update_columns(created_at: 2.days.ago)
newer = create(:tag, :nico)
newer.tag_name.update!(name: 'nico:b')
newer.update_columns(created_at: 1.day.ago)
older_post_tag =
PostTag.create!(post: Post.create!(url: 'https://example.com/nico-older'), tag: older)
older_post_tag.update_columns(created_at: 1.hour.ago)
newer_post_tag =
PostTag.create!(post: Post.create!(url: 'https://example.com/nico-newer'), tag: newer)
newer_post_tag.update_columns(created_at: 2.hours.ago)
get '/tags/nico', params: { order: 'name:desc' }
expect(json.fetch('tags').map { |tag| tag['id'] }).to eq([newer.id, older.id])
get '/tags/nico', params: { order: 'created_at:asc' }
expect(json.fetch('tags').map { |tag| tag['id'] }).to eq([older.id, newer.id])
get '/tags/nico', params: { order: 'updated_at:desc' }
expect(json.fetch('tags').map { |tag| tag['id'] }).to eq([older.id, newer.id])
expect(Time.zone.parse(json.fetch('tags').first.fetch('recent_post_tag_created_at')))
.to be_within(1.second).of(older_post_tag.created_at)
end
end
@@ -75,7 +131,7 @@ RSpec.describe 'NicoTags', type: :request do
expect(versions.last.created_by_user_id).to eq(admin.id)
end
it '400 when linked tag normalises to nico tag' do
it 'returns 422 when linked tag normalises to nico tag' do
sign_in_as(member)
other_nico = create(:tag, :nico, name: 'nico:linked_ng')
@@ -87,7 +143,37 @@ RSpec.describe 'NicoTags', type: :request do
patch "/tags/nico/#{nico_tag.id}", params: { tags: 'linked_ng_alias' }
}.not_to change(NicoTagVersion, :count)
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tags' => ['ニコニコ・タグ同士は連携できません.'])
end
it 'returns the tags field error when a nico tag is specified directly' do
sign_in_as(member)
patch "/tags/nico/#{nico_tag.id}", params: { tags: 'nico:linked_ng' }
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tags' => ['ニコニコ・タグ同士は連携できません.'])
end
it 'returns tag name validation errors on the tags field and rolls back created tags' do
sign_in_as(member)
TagNameSanitisationRule.create!(
priority: 1,
source_pattern: 'invalid',
replacement: 'valid'
)
nico_tag
expect {
patch "/tags/nico/#{nico_tag.id}", params: { tags: 'created_first invalid' }
}.not_to change(TagName, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors').fetch('tags')).to include(
a_string_including('タグ名 “invalid”:', '名前に使用できない文字が含まれてゐます.'))
end
end
end
+176 -4
ファイルの表示
@@ -57,6 +57,23 @@ RSpec.describe 'Posts API', type: :request do
post_write_params({ base_version_no: base_version.version_no }.merge(params))
end
def count_sql_queries
count = 0
callback = lambda do |_name, _started, _finished, _id, payload|
next if payload[:cached]
next if ['SCHEMA', 'TRANSACTION'].include?(payload[:name])
count += 1
end
ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
yield
end
count
end
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
@@ -500,6 +517,24 @@ RSpec.describe 'Posts API', type: :request do
expect([true, false]).to include(json['viewed'])
end
it 'omits deprecated tags' do
deprecated_tag = Tag.create!(
name: 'deprecated_post_tag',
category: :general,
deprecated_at: Time.current
)
PostTag.create!(post: post_record, tag: deprecated_tag)
request
expect(response).to have_http_status(:ok)
tag_names = json.fetch('tags').flat_map { |node|
[node.fetch('name')] + node.fetch('children').map { |child| child.fetch('name') }
}
expect(tag_names).to include('spec_tag')
expect(tag_names).not_to include('deprecated_post_tag')
end
context 'when post has parent, child, and sibling posts' do
let!(:parent_post) do
create_parent_post!(
@@ -558,6 +593,59 @@ RSpec.describe 'Posts API', type: :request do
expect(sibling_ids).to include(sibling_post.id)
end
end
it 'does not issue a query per tag or related post' do
user = create_member_user!
tags =
15.times.map do |i|
tag_name = TagName.create!(name: "show_query_tag_#{ i }")
tag = Tag.create!(tag_name:, category: :general)
TagName.create!(name: "show_query_alias_#{ i }", canonical: tag_name)
PostTag.create!(post: post_record, tag:)
tag
end
tags.each_cons(2) do |parent_tag, child_tag|
TagImplication.create!(parent_tag:, tag: child_tag)
end
parent_post = Post.create!(
title: 'query parent post',
url: 'https://example.com/query-parent-post'
)
sibling_post = Post.create!(
title: 'query sibling post',
url: 'https://example.com/query-sibling-post'
)
child_post = Post.create!(
title: 'query child post',
url: 'https://example.com/query-child-post'
)
PostImplication.create!(post: post_record, parent_post:)
PostImplication.create!(post: sibling_post, parent_post:)
PostImplication.create!(post: child_post, parent_post: post_record)
20.times do |i|
related_post = Post.create!(
title: "query related post #{ i }",
url: "https://example.com/query-related-post-#{ i }"
)
PostSimilarity.create!(post: post_record,
target_post: related_post,
cos: 1.0 - (i / 100.0))
end
query_count =
count_sql_queries do
get "/posts/#{ post_record.id }",
headers: { 'X-Transfer-Code' => user.inheritance_code }
end
expect(response).to have_http_status(:ok)
expect(query_count).to be <= 45
end
end
context 'when post does not exist' do
@@ -627,6 +715,58 @@ RSpec.describe 'Posts API', type: :request do
expect(names).not_to include('manko')
end
it 'rejects a deprecated tag specified directly' do
Tag.create!(
name: 'deprecated_direct_tag',
category: :general,
deprecated_at: Time.current
)
sign_in_as(member)
post '/posts', params: post_write_params(
title: 'new post',
url: 'https://example.com/deprecated-direct-tag',
tags: 'deprecated_direct_tag',
thumbnail: dummy_upload
)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tags' => ['廃止済みタグは付与できません.']
)
end
it 'expands through multiple deprecated parent tags and saves active ancestors' do
child = Tag.create!(name: 'active_child', category: :general)
deprecated_parent = Tag.create!(
name: 'deprecated_parent',
category: :general,
deprecated_at: Time.current
)
deprecated_grandparent = Tag.create!(
name: 'deprecated_grandparent',
category: :general,
deprecated_at: Time.current
)
active_grandparent = Tag.create!(name: 'active_grandparent', category: :general)
TagImplication.create!(tag: child, parent_tag: deprecated_parent)
TagImplication.create!(tag: deprecated_parent, parent_tag: deprecated_grandparent)
TagImplication.create!(tag: deprecated_grandparent, parent_tag: active_grandparent)
sign_in_as(member)
post '/posts', params: post_write_params(
title: 'expanded post',
url: 'https://example.com/expanded-deprecated-parent',
tags: 'active_child',
thumbnail: dummy_upload
)
expect(response).to have_http_status(:created)
saved_names = Post.find(json.fetch('id')).tags.map(&:name)
expect(saved_names).to include('active_child', 'active_grandparent')
expect(saved_names).not_to include('deprecated_parent', 'deprecated_grandparent')
end
context "when nico tag already exists in tags" do
before do
Tag.find_undiscard_or_create_by!(
@@ -634,7 +774,7 @@ RSpec.describe 'Posts API', type: :request do
category: :nico)
end
it 'return 400' do
it 'returns 422 with tag field errors' do
sign_in_as(member)
post '/posts', params: post_write_params(
@@ -644,7 +784,13 @@ RSpec.describe 'Posts API', type: :request do
thumbnail: dummy_upload
)
expect(response).to have_http_status(:bad_request), response.body
expect(response).to have_http_status(:unprocessable_entity), response.body
expect(json).to include(
'type' => 'validation_error',
'message' => '入力内容を確認してください.',
'base_errors' => [])
expect(json.fetch('errors')).to include(
'tags' => ['ニコニコ・タグは直接指定できません.'])
end
end
@@ -854,6 +1000,26 @@ RSpec.describe 'Posts API', type: :request do
expect(names).to include('spec_tag_2')
end
it 'rejects a deprecated tag specified directly' do
Tag.create!(
name: 'deprecated_update_tag',
category: :general,
deprecated_at: Time.current
)
sign_in_as(member)
put "/posts/#{ post_record.id }", params: post_update_params(
post_record,
title: 'updated title',
tags: 'deprecated_update_tag'
)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'tags' => ['廃止済みタグは付与できません.']
)
end
context "when nico tag already exists in tags" do
before do
Tag.find_undiscard_or_create_by!(
@@ -861,7 +1027,7 @@ RSpec.describe 'Posts API', type: :request do
category: :nico)
end
it 'return 400' do
it 'returns 422 with tag field errors' do
sign_in_as(member)
put "/posts/#{post_record.id}", params: post_update_params(
@@ -869,7 +1035,13 @@ RSpec.describe 'Posts API', type: :request do
title: 'updated title',
tags: 'nico:nico_tag')
expect(response).to have_http_status(:bad_request), response.body
expect(response).to have_http_status(:unprocessable_entity), response.body
expect(json).to include(
'type' => 'validation_error',
'message' => '入力内容を確認してください.',
'base_errors' => [])
expect(json.fetch('errors')).to include(
'tags' => ['ニコニコ・タグは直接指定できません.'])
end
end
+11
ファイルの表示
@@ -21,6 +21,7 @@ RSpec.describe 'TagVersions API', type: :request do
event_type:,
name:,
category:,
deprecated_at: nil,
aliases: [],
parent_tags: [],
created_by_user:,
@@ -33,6 +34,7 @@ RSpec.describe 'TagVersions API', type: :request do
event_type: event_type,
name: name,
category: category,
deprecated_at: deprecated_at,
aliases: Array(aliases).join(' '),
parent_tag_ids: Array(parent_tags).map(&:id).join(' '),
created_by_user: created_by_user,
@@ -65,6 +67,7 @@ RSpec.describe 'TagVersions API', type: :request do
event_type: 'update',
name: 'new_tag_name',
category: 'meme',
deprecated_at: t_v2,
aliases: ['alias_shared', 'alias_new'],
parent_tags: [parent_shared, parent_new],
created_by_user: member,
@@ -133,6 +136,10 @@ RSpec.describe 'TagVersions API', type: :request do
'current' => 'meme',
'prev' => 'general'
)
expect(latest.fetch('deprecated_at')).to eq(
'current' => t_v2.iso8601,
'prev' => nil
)
expect(latest.fetch('aliases')).to include(
{ 'name' => 'alias_shared', 'type' => 'context' },
{ 'name' => 'alias_new', 'type' => 'added' },
@@ -178,6 +185,10 @@ RSpec.describe 'TagVersions API', type: :request do
'current' => 'general',
'prev' => nil
)
expect(first.fetch('deprecated_at')).to eq(
'current' => nil,
'prev' => nil
)
expect(first.fetch('aliases')).to include(
{ 'name' => 'alias_shared', 'type' => 'added' },
{ 'name' => 'alias_old', 'type' => 'added' }
+3
ファイルの表示
@@ -89,6 +89,7 @@ RSpec.describe 'Tag and wiki history integrity', type: :request do
category: 'general',
aliases: '',
parent_tags: '',
deprecated: '0',
}
}
.to change(TagVersion, :count).by(2)
@@ -123,6 +124,7 @@ RSpec.describe 'Tag and wiki history integrity', type: :request do
category: 'meme',
aliases: '',
parent_tags: '',
deprecated: '0',
}
}
.to change(TagVersion, :count).by(2)
@@ -149,6 +151,7 @@ RSpec.describe 'Tag and wiki history integrity', type: :request do
category: 'general',
aliases: 'put_tag_alias_only_alias',
parent_tags: '',
deprecated: '0',
}
}
.to change(TagVersion, :count).by(2)
+24
ファイルの表示
@@ -275,6 +275,30 @@ RSpec.describe 'Tags deerjikists API', type: :request do
end
end
context 'when a row is invalid' do
let(:payload) do
[
{ platform: '', code: code1 },
]
end
it 'returns 422 with indexed field errors and does not replace existing deerjikists' do
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
expect {
do_request
}.not_to change { Deerjikist.where(tag: tag).map { |d| [d.platform, d.code] } }
expect(response).to have_http_status(:unprocessable_entity)
expect(json).to include(
'type' => 'validation_error',
'message' => '入力内容を確認してください.',
'base_errors' => [])
expect(json.fetch('errors')).to include(
'deerjikists.0.platform' => [be_present])
end
end
context 'when youtube code is handle' do
let(:channel_id) { 'UCabcdefghijklmnopqrstuv' }
let(:payload) do
+193
ファイルの表示
@@ -76,6 +76,27 @@ RSpec.describe 'Tags API', type: :request do
expect(response_tags.first['id']).to eq(meme.id)
end
it 'filters tags by deprecated state' do
deprecated_tag = Tag.create!(
name: 'deprecated_filter',
category: :general,
deprecated_at: 1.day.from_now
)
active_tag = Tag.create!(name: 'active_filter', category: :general)
get '/tags', params: { name: '_filter', deprecated: '1' }
expect(response).to have_http_status(:ok)
expect(response_names).to include(deprecated_tag.name)
expect(response_names).not_to include(active_tag.name)
get '/tags', params: { name: '_filter', deprecated: '0' }
expect(response).to have_http_status(:ok)
expect(response_names).to include(active_tag.name)
expect(response_names).not_to include(deprecated_tag.name)
end
it 'filters tags by post_count range' do
low = Tag.create!(tag_name: TagName.create!(name: 'pc_low'), category: :general)
mid = Tag.create!(tag_name: TagName.create!(name: 'pc_mid'), category: :general)
@@ -301,6 +322,21 @@ RSpec.describe 'Tags API', type: :request do
expect(t['matched_alias']).to eq('unko')
expect(json.map { |x| x['name'] }).not_to include('unknown')
end
it 'omits deprecated tags' do
deprecated_tag = Tag.create!(
name: 'spec_deprecated',
category: :general,
deprecated_at: Time.current
)
deprecated_tag.update_columns(post_count: 1)
get '/tags/autocomplete', params: { q: 'spec_', present: '0' }
expect(response).to have_http_status(:ok)
expect(json.map { |item| item.fetch('name') }).to include('spec_tag')
expect(json.map { |item| item.fetch('name') }).not_to include('spec_deprecated')
end
end
describe 'GET /tags/name/:name' do
@@ -437,6 +473,32 @@ RSpec.describe 'Tags API', type: :request do
expect(versions.second.created_by_user_id).to eq(member_user.id)
end
it 'updates deprecated state and records it in tag versions' do
expect {
patch "/tags/#{ tag.id }", params: { deprecated: '1' }
}.to change(TagVersion, :count).by(2)
expect(response).to have_http_status(:ok)
expect(tag.reload.deprecated_at).to be_present
versions = tag.tag_versions.order(:version_no)
expect(versions.first.deprecated_at).to be_nil
expect(versions.second.deprecated_at).to eq(tag.deprecated_at)
expect(json.fetch('deprecated_at')).to be_present
end
it 'rejects deprecating a nico tag' do
nico_tag = Tag.create!(name: 'nico:deprecated_update', category: :nico)
patch "/tags/#{ nico_tag.id }", params: { deprecated: '1' }
expect(response).to have_http_status(:unprocessable_entity)
expect(nico_tag.reload.deprecated_at).to be_nil
expect(json.fetch('errors')).to include(
'deprecated' => ['ニコタグは廃止できません.']
)
end
it 'returns 422 when changing normal tag category to nico' do
expect {
patch "/tags/#{tag.id}", params: { category: 'nico' }
@@ -585,6 +647,111 @@ RSpec.describe 'Tags API', type: :request do
expect(row['has_children']).to eq(true)
expect(row['children']).to eq([])
end
it 'passes through deprecated tags when finding children' do
deprecated_middle = Tag.create!(
name: 'depth_deprecated_middle',
category: :character,
deprecated_at: Time.current
)
visible_descendant = Tag.create!(
name: 'depth_visible_descendant',
category: :material
)
TagImplication.create!(parent_tag: root_material, tag: deprecated_middle)
TagImplication.create!(parent_tag: deprecated_middle, tag: visible_descendant)
get '/tags/with-depth', params: { parent: root_material.id }
expect(response).to have_http_status(:ok)
expect(json.map { |item| item.fetch('name') }).to eq(['depth_visible_descendant'])
expect(json.map { |item| item.fetch('name') }).not_to include('depth_deprecated_middle')
end
it 'passes through multiple deprecated tags for roots and has_children' do
active_child = Tag.create!(
name: 'depth_active_child_below_deprecated',
category: :character
)
deprecated_parent = Tag.create!(
name: 'depth_deprecated_parent',
category: :character,
deprecated_at: Time.current
)
deprecated_grandparent = Tag.create!(
name: 'depth_deprecated_grandparent',
category: :material,
deprecated_at: Time.current
)
active_ancestor = Tag.create!(
name: 'depth_active_ancestor',
category: :meme
)
TagImplication.create!(tag: active_child, parent_tag: deprecated_parent)
TagImplication.create!(tag: deprecated_parent, parent_tag: deprecated_grandparent)
TagImplication.create!(tag: deprecated_grandparent, parent_tag: active_ancestor)
get '/tags/with-depth'
root_names = json.map { |item| item.fetch('name') }
expect(root_names).to include('depth_active_ancestor')
expect(root_names).not_to include('depth_active_child_below_deprecated')
ancestor_json = json.find { |item| item.fetch('id') == active_ancestor.id }
expect(ancestor_json.fetch('has_children')).to eq(true)
get '/tags/with-depth', params: { parent: active_ancestor.id }
expect(json.map { |item| item.fetch('name') }).to include(
'depth_active_child_below_deprecated'
)
expect(json.map { |item| item.fetch('name') }).not_to include(
'depth_deprecated_parent',
'depth_deprecated_grandparent'
)
end
it 'treats an active tag with only deprecated ancestors as a root' do
active_child = Tag.create!(
name: 'depth_root_below_deprecated',
category: :character
)
deprecated_parent = Tag.create!(
name: 'depth_root_deprecated_parent',
category: :material,
deprecated_at: Time.current
)
TagImplication.create!(tag: active_child, parent_tag: deprecated_parent)
get '/tags/with-depth'
expect(json.map { |item| item.fetch('name') }).to include(
'depth_root_below_deprecated'
)
expect(json.map { |item| item.fetch('name') }).not_to include(
'depth_root_deprecated_parent'
)
end
it 'terminates when deprecated implications contain a cycle' do
first = Tag.create!(
name: 'depth_cycle_first',
category: :character,
deprecated_at: Time.current
)
second = Tag.create!(
name: 'depth_cycle_second',
category: :material,
deprecated_at: Time.current
)
TagImplication.create!(tag: first, parent_tag: root_material)
TagImplication.create!(tag: second, parent_tag: first)
TagImplication.create!(tag: first, parent_tag: second)
get '/tags/with-depth', params: { parent: root_material.id }
expect(response).to have_http_status(:ok)
expect(json).to eq([])
end
end
describe 'GET /tags/name/:name/materials' do
@@ -732,6 +899,20 @@ RSpec.describe 'Tags API', type: :request do
expect(tag.category).to eq('general')
end
it 'deprecated がなければ 422 を返す' do
put "/tags/#{ tag.id }", params: {
name: 'new',
category: 'general',
aliases: '',
parent_tags: '',
}
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'deprecated' => ['廃止状態は必須です.']
)
end
it 'name, category, aliases, parent tags をまとめて更新できる' do
old_parent = Tag.create!(
tag_name: TagName.create!(name: 'put_old_parent'),
@@ -749,6 +930,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'meme',
aliases: 'put_alias_a put_alias_b put_alias_a',
parent_tags: 'put_kept_parent put_new_parent',
deprecated: '0',
}
expect(response).to have_http_status(:ok)
@@ -793,6 +975,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'spec_tag put_alias_self_test',
parent_tags: '',
deprecated: '0',
}
expect(response).to have_http_status(:ok)
@@ -810,6 +993,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'unko',
parent_tags: 'spec_tag',
deprecated: '0',
}
expect(response).to have_http_status(:ok)
@@ -825,6 +1009,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'meta',
aliases: '',
parent_tags: '',
deprecated: '0',
}
}.to change(TagVersion, :count).by(2)
@@ -860,6 +1045,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'unko',
parent_tags: new_parent.name,
deprecated: '0',
}
expect(response).to have_http_status(:ok)
@@ -875,6 +1061,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'nico',
aliases: '',
parent_tags: '',
deprecated: '0',
}
}.not_to change(TagVersion, :count)
@@ -896,6 +1083,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'nico',
aliases: '',
parent_tags: '',
deprecated: '0',
}
}.not_to change(NicoTagVersion, :count)
@@ -916,6 +1104,7 @@ RSpec.describe 'Tags API', type: :request do
category: old_category,
aliases: '',
parent_tags: '',
deprecated: '0',
}
}.not_to change(TagVersion, :count)
@@ -946,6 +1135,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'meme',
aliases: 'unko',
parent_tags: '',
deprecated: '0',
}
}
.to change(TagVersion, :count).by(2)
@@ -981,6 +1171,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'unko put_stolen_alias',
parent_tags: '',
deprecated: '0',
}
}
.to change { tag.reload.tag_versions.count }.by(2)
@@ -1015,6 +1206,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'unko',
parent_tags: child.name,
deprecated: '0',
}
expect(response).to have_http_status(:unprocessable_entity)
@@ -1036,6 +1228,7 @@ RSpec.describe 'Tags API', type: :request do
category: 'general',
aliases: 'unko',
parent_tags: '',
deprecated: '0',
}
}
.to change(TagVersion, :count).by(2)
+60
ファイルの表示
@@ -80,6 +80,26 @@ RSpec.describe 'TheatreComments', type: :request do
expect(response).to have_http_status(:ok)
expect(response.parsed_body.map { |row| row['no'] }).to eq([3, 2, 1])
end
it '削除済みコメントは deleted として返し、本文を隠す' do
comment_2.discard!
get "/theatres/#{theatre.id}/comments", params: { no_gt: 1 }
expect(response).to have_http_status(:ok)
deleted_comment = response.parsed_body.find { _1['no'] == 2 }
expect(deleted_comment).to include(
'deleted' => true,
'content' => nil
)
visible_comment = response.parsed_body.find { _1['no'] == 3 }
expect(visible_comment).to include(
'deleted' => false,
'content' => 'third comment'
)
end
end
describe 'POST /theatres/:theatre_id/comments' do
@@ -147,4 +167,44 @@ RSpec.describe 'TheatreComments', type: :request do
})
end
end
describe 'DELETE /theatres/:theatre_id/comments/:id' do
let(:theatre) { create(:theatre) }
let(:alice) { create(:user, name: 'Alice') }
let(:bob) { create(:user, name: 'Bob') }
let!(:comment) do
create(
:theatre_comment,
theatre: theatre,
no: 1,
user: alice,
content: 'delete target'
)
end
it 'returns 401 when not logged in' do
delete "/theatres/#{theatre.id}/comments/#{comment.no}"
expect(response).to have_http_status(:unauthorized)
expect(comment.reload.discarded?).to eq(false)
end
it 'allows the comment owner to delete it' do
sign_in_as(alice)
delete "/theatres/#{theatre.id}/comments/#{comment.no}"
expect(response).to have_http_status(:no_content)
expect(comment.reload.discarded?).to eq(true)
end
it 'returns 403 when another user tries to delete it' do
sign_in_as(bob)
delete "/theatres/#{theatre.id}/comments/#{comment.no}"
expect(response).to have_http_status(:forbidden)
expect(comment.reload.discarded?).to eq(false)
end
end
end
+38
ファイルの表示
@@ -0,0 +1,38 @@
require 'rails_helper'
RSpec.describe 'TheatreProgrammes', type: :request do
describe 'GET /theatres/:theatre_id/programmes' do
let(:theatre) { create(:theatre) }
let(:other_theatre) { create(:theatre) }
let(:post_1) { Post.create!(title: 'first', url: 'https://www.nicovideo.jp/watch/sm1') }
let(:post_2) { Post.create!(title: 'second', url: 'https://www.nicovideo.jp/watch/sm2') }
let(:other_post) { Post.create!(title: 'other', url: 'https://www.nicovideo.jp/watch/sm3') }
before do
TheatreProgramme.create!(theatre:, position: 1, post: post_1, created_at: 2.minutes.ago)
TheatreProgramme.create!(theatre:, position: 2, post: post_2, created_at: 1.minute.ago)
TheatreProgramme.create!(
theatre: other_theatre,
position: 1,
post: other_post,
created_at: 1.minute.ago
)
end
it 'returns programmes for the theatre in descending position with post json' do
get "/theatres/#{theatre.id}/programmes"
expect(response).to have_http_status(:ok)
expect(json.map { _1['position'] }).to eq([2, 1])
expect(json.map { _1.dig('post', 'title') }).to eq(['second', 'first'])
expect(json.first['post']).to include('id' => post_2.id, 'url' => post_2.url)
end
it 'filters programmes by position_gt' do
get "/theatres/#{theatre.id}/programmes", params: { position_gt: 1 }
expect(response).to have_http_status(:ok)
expect(json.map { _1['position'] }).to eq([2])
end
end
end
+226 -8
ファイルの表示
@@ -14,10 +14,24 @@ RSpec.describe 'Theatres API', type: :request do
let(:member) { create(:user, :member, name: 'member user') }
let(:other_user) { create(:user, :member, name: 'other user') }
let!(:niconico_post) do
Post.create!(
title: 'niconico post',
url: 'https://www.nicovideo.jp/watch/sm123'
)
end
let!(:second_niconico_post) do
Post.create!(
title: 'second niconico post',
url: 'https://www.nicovideo.jp/watch/sm456'
)
end
let!(:youtube_post) do
Post.create!(
title: 'youtube post',
url: 'https://www.youtube.com/watch?v=spec123'
url: 'https://www.youtube.com/watch?v=yt123'
)
end
@@ -120,7 +134,8 @@ RSpec.describe 'Theatres API', type: :request do
expect(json).to include(
'host_flg' => true,
'post_id' => nil,
'post_started_at' => nil
'post_started_at' => nil,
'post_elapsed_ms' => nil
)
expect(json.fetch('watching_users')).to contain_exactly(
@@ -177,7 +192,8 @@ RSpec.describe 'Theatres API', type: :request do
expect(json).to include(
'host_flg' => false,
'post_id' => nil,
'post_started_at' => nil
'post_started_at' => nil,
'post_elapsed_ms' => nil
)
expect(json.fetch('watching_users')).to contain_exactly(
@@ -204,7 +220,7 @@ RSpec.describe 'Theatres API', type: :request do
)
theatre.update!(
host_user: other_user,
current_post: youtube_post,
current_post: niconico_post,
current_post_started_at: started_at
)
sign_in_as(member)
@@ -220,9 +236,11 @@ RSpec.describe 'Theatres API', type: :request do
expect(theatre.host_user_id).to eq(member.id)
expect(json['host_flg']).to eq(true)
expect(json['post_id']).to eq(youtube_post.id)
expect(json['post_id']).to eq(niconico_post.id)
expect(Time.zone.parse(json['post_started_at']))
.to be_within(1.second).of(started_at)
expect(json['post_elapsed_ms'])
.to be_within(1_000).of(120_000)
end
end
end
@@ -273,16 +291,36 @@ RSpec.describe 'Theatres API', type: :request do
it 'sets current_post to an eligible post and updates current_post_started_at' do
expect { do_request }
.to change { theatre.reload.current_post_id }
.from(nil).to(youtube_post.id)
expect(response).to have_http_status(:no_content)
expect([niconico_post.id, second_niconico_post.id, youtube_post.id])
.to include(theatre.reload.current_post_id)
expect(theatre.reload.current_post_started_at)
.to be_within(1.second).of(Time.current)
expect(theatre.programmes.count).to eq(1)
end
end
context 'when only a YouTube post is eligible' do
before do
niconico_post.destroy!
second_niconico_post.destroy!
theatre.update!(host_user: member)
sign_in_as(member)
end
it 'sets current_post to the YouTube post' do
do_request
expect(response).to have_http_status(:no_content)
expect(theatre.reload.current_post_id).to eq(youtube_post.id)
end
end
context 'when current user is host and no eligible post exists' do
before do
niconico_post.destroy!
second_niconico_post.destroy!
youtube_post.destroy!
theatre.update!(
host_user: member,
@@ -299,9 +337,189 @@ RSpec.describe 'Theatres API', type: :request do
theatre.reload
expect(theatre.current_post_id).to be_nil
expect(theatre.current_post_started_at)
.to be_within(1.second).of(Time.current)
expect(theatre.current_post_started_at).to be_nil
end
end
end
describe 'PUT /theatres/:id/skip_vote' do
subject(:do_request) do
put "/theatres/#{theatre.id}/skip_vote", params: { post_id: requested_post_id }
end
let(:third_user) { create(:user, :member, name: 'third user') }
let(:requested_post_id) { niconico_post.id }
before do
theatre.update!(current_post: niconico_post, current_post_started_at: 10.seconds.ago)
[member, other_user, third_user].each do |user|
TheatreWatchingUser.create!(
theatre:,
user:,
expires_at: 10.seconds.from_now
)
end
end
it 'returns 401 when not logged in' do
sign_out
expect { do_request }.not_to change(TheatreSkipVote, :count)
expect(response).to have_http_status(:unauthorized)
end
it 'returns 422 when post_id is invalid' do
sign_in_as(member)
expect {
put "/theatres/#{theatre.id}/skip_vote", params: { post_id: 'invalid' }
}.not_to change(TheatreSkipVote, :count)
expect(response).to have_http_status(:unprocessable_entity)
end
it 'records a vote and returns the current vote status before majority' do
sign_in_as(member)
expect { do_request }.to change(TheatreSkipVote, :count).by(1)
expect(response).to have_http_status(:ok)
expect(json['skipped']).to eq(false)
expect(json['post_id']).to eq(niconico_post.id)
expect(json['skip_vote']).to include(
'votes_count' => 1,
'required_count' => 2,
'watching_users_count' => 3,
'voted' => true
)
end
it 'finalizes skip when votes reach majority and stores voters and tag snapshots' do
tag = create(:tag, name: 'skip-target')
PostTag.create!(post: niconico_post, tag:)
TheatreSkipVote.create!(theatre:, post: niconico_post, user: member)
sign_in_as(other_user)
expect { do_request }
.to change(TheatreSkipEvent, :count).by(1)
.and change(TheatreSkipEventVoter, :count).by(2)
.and change(TheatreSkipEventTag, :count).by(1)
expect(response).to have_http_status(:ok)
expect(json['skipped']).to eq(true)
expect([second_niconico_post.id, youtube_post.id]).to include(json['post_id'])
event = TheatreSkipEvent.last
expect(event.post).to eq(niconico_post)
expect(event.users).to contain_exactly(member, other_user)
expect(event.tags).to contain_exactly(tag)
expect(TheatreSkipVote.where(theatre:, post: niconico_post)).to be_empty
end
it 'does not record a vote when requested post is no longer current' do
theatre.update!(current_post: second_niconico_post)
sign_in_as(member)
expect { do_request }.not_to change(TheatreSkipVote, :count)
expect(response).to have_http_status(:conflict)
expect(json['post_id']).to eq(second_niconico_post.id)
expect(json['skip_vote']).to include(
'votes_count' => 0,
'voted' => false
)
end
end
describe 'DELETE /theatres/:id/skip_vote' do
let(:requested_post_id) { niconico_post.id }
before do
theatre.update!(current_post: niconico_post, current_post_started_at: 10.seconds.ago)
TheatreWatchingUser.create!(theatre:, user: member, expires_at: 10.seconds.from_now)
TheatreSkipVote.create!(theatre:, post: niconico_post, user: member)
sign_in_as(member)
end
it 'removes the current user vote' do
expect {
delete "/theatres/#{theatre.id}/skip_vote", params: { post_id: requested_post_id }
}.to change(TheatreSkipVote, :count).by(-1)
expect(response).to have_http_status(:ok)
expect(json['skip_vote']).to include(
'votes_count' => 0,
'required_count' => 1,
'watching_users_count' => 1,
'voted' => false
)
end
it 'does not remove a vote when requested post is no longer current' do
theatre.update!(current_post: second_niconico_post)
expect {
delete "/theatres/#{theatre.id}/skip_vote", params: { post_id: requested_post_id }
}.not_to change(TheatreSkipVote, :count)
expect(response).to have_http_status(:conflict)
expect(json['post_id']).to eq(second_niconico_post.id)
end
end
describe 'GET /theatres/:id/skip_events' do
before do
sign_in_as(member)
end
it 'does not expose skip voters' do
event = TheatreSkipEvent.create!(
theatre:,
post: niconico_post,
skipped_by_user: member,
created_at: Time.current
)
TheatreSkipEventVoter.create!(theatre_skip_event: event, user: member)
get "/theatres/#{theatre.id}/skip_events"
expect(response).to have_http_status(:ok)
expect(json.first).to include(
'id' => event.id,
'theatre_id' => theatre.id
)
expect(json.first).not_to have_key('voters')
expect(json.first).not_to have_key('skipped_by_user')
end
end
describe 'GET /theatres/:id/post_selection_weights' do
before do
theatre.update!(current_post: niconico_post)
TheatreWatchingUser.create!(theatre:, user: member, expires_at: 10.seconds.from_now)
sign_in_as(member)
end
it 'returns tag penalties and candidate weights for the current watchers' do
tag = create(:tag, name: 'heavy-tag')
PostTag.create!(post: second_niconico_post, tag:)
event = TheatreSkipEvent.create!(
theatre:,
post: niconico_post,
skipped_by_user: member,
created_at: Time.current
)
TheatreSkipEventVoter.create!(theatre_skip_event: event, user: member)
TheatreSkipEventTag.create!(theatre_skip_event: event, tag:)
get "/theatres/#{theatre.id}/post_selection_weights"
expect(response).to have_http_status(:ok)
expect(json['tag_penalties'].first['penalty']).to eq(1)
expect(json['lightest_posts'].first['post']['id']).to eq(second_niconico_post.id)
expect(json['lightest_posts'].first['penalty']).to eq(1)
end
end
end
+4 -2
ファイルの表示
@@ -90,12 +90,14 @@ RSpec.describe 'Users', type: :request do
expect(response).to have_http_status(:unauthorized)
end
it 'returns 400 when name is blank' do
it 'returns 422 when name is blank' do
put "/users/#{user.id}",
params: { name: ' ' },
headers: auth_headers(user)
expect(response).to have_http_status(:bad_request)
expect(response).to have_http_status(:unprocessable_entity)
expect(json.fetch('errors')).to include(
'name' => ['名前は必須です.'])
end
it 'updates name and returns user slice' do
+17 -2
ファイルの表示
@@ -18,6 +18,13 @@ RSpec.describe 'Wiki API', type: :request do
created_by_user: user,
message: 'init')
end
let!(:tag) do
Tag.create!(
tag_name: tn,
category: :general,
deprecated_at: Time.zone.local(2026, 6, 1)
)
end
describe 'GET /wiki' do
it 'returns wiki pages with title' do
@@ -30,6 +37,8 @@ RSpec.describe 'Wiki API', type: :request do
expect(json[0]).to have_key('title')
expect(json.map { |p| p['title'] }).to include('spec_wiki_title')
wiki_json = json.find { |item| item.fetch('id') == page.id }
expect(wiki_json.fetch('deprecated_at')).to eq(tag.deprecated_at.iso8601(3))
end
end
@@ -48,7 +57,8 @@ RSpec.describe 'Wiki API', type: :request do
expect(json).to include(
'id' => page.id,
'title' => 'spec_wiki_title')
'title' => 'spec_wiki_title',
'deprecated_at' => tag.deprecated_at.iso8601(3))
end
end
@@ -409,7 +419,11 @@ RSpec.describe 'Wiki API', type: :request do
'kind' => 'content',
'message' => 'r2'
)
expect(top['wiki_page']).to include('id' => page.id, 'title' => 'spec_wiki_title')
expect(top['wiki_page']).to include(
'id' => page.id,
'title' => 'spec_wiki_title',
'deprecated_at' => tag.deprecated_at.iso8601(3)
)
expect(top['user']).to include('id' => user.id, 'name' => user.name)
expect(top).to have_key('timestamp')
@@ -479,6 +493,7 @@ RSpec.describe 'Wiki API', type: :request do
expect(json).to include(
'wiki_page_id' => page.id,
'title' => 'spec_wiki_title',
'deprecated_at' => tag.deprecated_at.iso8601(3),
'older_revision_id' => rev_a.id,
'newer_revision_id' => rev_b.id
)
+112
ファイルの表示
@@ -0,0 +1,112 @@
require 'rails_helper'
RSpec.describe Gekanator::QuestionSuggestionAiConverter do
let(:user) { create(:user, :member) }
let(:guessed_post) { Post.create!(title: 'guess', url: 'https://example.com/guess') }
let(:correct_post) { Post.create!(title: 'correct', url: 'https://example.com/correct') }
let(:game) do
GekanatorGame.create!(
user: user,
guessed_post: guessed_post,
correct_post: correct_post,
won: false,
question_count: 1,
answers: [{ 'question_id' => 'tag:1', 'answer' => 'yes' }]
)
end
def create_suggestion!(question_text:, answer: 'yes')
GekanatorQuestionSuggestion.create!(
gekanator_game: game,
user: user,
question_text: question_text,
answer: answer
)
end
it 'converts title-contains suggestions to pending ai-generated questions' do
suggestion = create_suggestion!(question_text: '題名に「結束バンド」が含まれる?')
expect {
described_class.call(suggestion: suggestion, user: user)
}.to change { GekanatorQuestion.count }.by(1)
.and change { GekanatorAiRun.count }.by(1)
question = GekanatorQuestion.last
expect(question).to have_attributes(
text: '題名に「結束バンド」が含まれる?',
kind: 'title',
source: 'ai_generated',
status: 'pending',
priority_weight: 0.95,
gekanator_question_suggestion_id: suggestion.id,
created_by_id: user.id
)
expect(question.condition).to include(
'type' => 'title-contains',
'text' => '結束バンド'
)
expect(GekanatorAiRun.last).to have_attributes(
gekanator_question_suggestion_id: suggestion.id,
model: 'heuristic_converter_v1',
status: 'succeeded'
)
end
it 'converts concrete non-unknown suggestions to post-similarity questions' do
suggestion = create_suggestion!(
question_text: '喜多ちゃんが泣いてる?',
answer: 'partial'
)
question = described_class.call(suggestion: suggestion, user: user)
expect(question).to have_attributes(
text: '喜多ちゃんが泣いてる?',
kind: 'post_similarity',
source: 'ai_generated',
status: 'pending',
priority_weight: 1.0
)
expect(question.condition).to include(
'type' => 'post-similarity',
'postId' => correct_post.id,
'answer' => 'partial',
'threshold' => 0.65
)
end
it 'records a failed run when the suggestion cannot be converted' do
suggestion = create_suggestion!(
question_text: 'よく分からない質問?',
answer: 'unknown'
)
expect {
expect(described_class.call(suggestion: suggestion, user: user)).to be_nil
}.not_to change { GekanatorQuestion.count }
expect(GekanatorAiRun.last).to have_attributes(
gekanator_question_suggestion_id: suggestion.id,
status: 'failed'
)
end
it 'returns an existing generated question without creating a duplicate run' do
suggestion = create_suggestion!(question_text: 'タイトルは 10 文字以上?')
existing = GekanatorQuestion.create!(
text: 'タイトルは 10 文字以上?',
kind: 'title',
source: 'ai_generated',
status: 'pending',
priority_weight: 0.95,
condition: { type: 'title-length-at-least', length: 10 },
gekanator_question_suggestion: suggestion,
created_by: user
)
expect {
expect(described_class.call(suggestion: suggestion, user: user)).to eq(existing)
}.not_to change { GekanatorAiRun.count }
end
end
+5 -2
ファイルの表示
@@ -4,11 +4,12 @@ require 'rails_helper'
RSpec.describe 'post_similarity:calc' do
include RakeTaskHelper
it 'calls Similarity::Calc with Post and :tags' do
it 'calculates similarities from active tags only' do
# 必要最低限のデータ
t1 = Tag.create!(name: "t1")
t2 = Tag.create!(name: "t2")
t3 = Tag.create!(name: "t3")
deprecated_tag = Tag.create!(name: 'deprecated', deprecated_at: Time.current)
p1 = Post.create!(url: "https://example.com/1")
p2 = Post.create!(url: "https://example.com/2")
@@ -22,6 +23,8 @@ RSpec.describe 'post_similarity:calc' do
PostTag.create!(post: p2, tag: t3)
PostTag.create!(post: p3, tag: t3)
PostTag.create!(post: p1, tag: deprecated_tag)
PostTag.create!(post: p2, tag: deprecated_tag)
expect { run_rake_task("post_similarity:calc") }
.to change { PostSimilarity.count }.from(0)
@@ -29,6 +32,6 @@ RSpec.describe 'post_similarity:calc' do
ps = PostSimilarity.find_by!(post_id: p1.id, target_post_id: p2.id)
ps_rev = PostSimilarity.find_by!(post_id: p2.id, target_post_id: p1.id)
expect(ps_rev.cos).to eq(ps.cos)
expect(ps.cos).to be_within(0.0001).of(0.5)
end
end
+5 -2
ファイルの表示
@@ -4,11 +4,12 @@ require 'rails_helper'
RSpec.describe 'tag_similarity:calc' do
include RakeTaskHelper
it 'calls Similarity::Calc with Tag and :posts' do
it 'calculates similarities for active tags only' do
# 必要最低限のデータ
t1 = Tag.create!(name: "t1")
t2 = Tag.create!(name: "t2")
t3 = Tag.create!(name: "t3")
deprecated_tag = Tag.create!(name: 'deprecated', deprecated_at: Time.current)
p1 = Post.create!(url: "https://example.com/1")
p2 = Post.create!(url: "https://example.com/2")
@@ -22,6 +23,7 @@ RSpec.describe 'tag_similarity:calc' do
PostTag.create!(post: p2, tag: t3)
PostTag.create!(post: p3, tag: t3)
PostTag.create!(post: p1, tag: deprecated_tag)
expect { run_rake_task("tag_similarity:calc") }
.to change { TagSimilarity.count }.from(0)
@@ -29,6 +31,7 @@ RSpec.describe 'tag_similarity:calc' do
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)
expect(TagSimilarity.where(tag_id: deprecated_tag.id)).to be_empty
expect(TagSimilarity.where(target_tag_id: deprecated_tag.id)).to be_empty
end
end