Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
test: review round 4 of #81, pin refusal texts, the author, the embedding and the MCP order
Guido Boyke1f7219184476
diff --git a/spec/locales/token_rule_refusal_texts_spec.rb b/spec/locales/token_rule_refusal_texts_spec.rbnew file mode 100644index 0000000..c183a74--- /dev/null+++ b/spec/locales/token_rule_refusal_texts_spec.rb@@ -0,0 +1,49 @@+# frozen_string_literal: true++require 'rails_helper'++# Guard: every refusal of a token write rule is worded on both surfaces, in+# both locales. Since REST R6 and R3a a rule names its refusals once+# (TimeSessions::Booking::REFUSALS, Posts::Comment::REFUSALS) and each hull+# words them: the MCP tool in its REFUSAL_TEXT, the HTTP API under a+# `services.*` scope in de and en.+#+# Why this exists (the drift it prevents): the three lists are kept by hand.+# A refusal added to a rule without its MCP text raises KeyError in the tool;+# without its locale key the API answers "Translation missing". The request+# specs could not see either while they read the expected text through the+# same key, and with the en->de fallback a key missing in English answers the+# German text on both sides.+#+# Reads the loaded backend rather than `I18n.exists?`, which that fallback+# answers with true for a key that exists only in German.+RSpec.describe 'Token write rule refusal texts' do+ # A new rule with refusals of its own adds a line here.+ rules = [+ { rule: Posts::Comment, tool: Lalabase::Tools::PostComment, scope: %i[services post_comment] },+ { rule: TimeSessions::Booking, tool: Lalabase::Tools::CreateTimeSession, scope: %i[services time_session_booking] }+ ].freeze++ # nil when absent, never the fallback value of another locale.+ def stored(locale, *path)+ I18n.backend.send(:translations)[locale]&.dig(*path)+ end++ before { I18n.backend.send(:init_translations) unless I18n.backend.initialized? }++ rules.each do |entry|+ refusals = entry[:rule]::REFUSALS++ it "words every refusal of #{entry[:rule]} in #{entry[:tool]}, and no other" do+ expect(entry[:tool]::REFUSAL_TEXT.keys).to match_array(refusals)+ end++ %i[de en].each do |locale|+ it "has a #{locale} text for every refusal of #{entry[:rule]}, and no other" do+ texts = stored(locale, *entry[:scope]) || {}+ expect(texts.keys).to match_array(refusals)+ expect(texts.values).to all(be_present)+ end+ end+ end+enddiff --git a/spec/requests/api/v1/post_comment_format_spec.rb b/spec/requests/api/v1/post_comment_format_spec.rbindex 01f8fb1..c20300e 100644--- a/spec/requests/api/v1/post_comment_format_spec.rb+++ b/spec/requests/api/v1/post_comment_format_spec.rb@@ -74,8 +74,21 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request dodef saved = ActsAsTenant.without_tenant { Post.find(body.dig('data', 'id')) }+ # Written out, not read through I18n.t as the controller reads them: a missing+ # or misspelt key would be compared with itself, and an English key that is+ # missing falls back to the German text on both sides.def refusal(key, locale: :en)- { 'error' => 'validation_failed', 'code' => 'invalid_input', 'errors' => [I18n.t("services.post_comment.#{key}", locale: locale)] }+ text = {+ en: {+ format_not_string: 'Give the format (post[format]) as text: html or plain.',+ format_unknown: 'The format (post[format]) must be html or plain.',+ body_invalid_utf8: 'The comment (post[body]) cannot be read as UTF-8.',+ body_nul: 'A plain comment (post[body]) must not contain NUL bytes.',+ body_empty: 'The comment (post[body]) has no text.'+ },+ de: { format_unknown: 'Das Format (post[format]) muss html oder plain sein.' }+ }.fetch(locale).fetch(key)+ { 'error' => 'validation_failed', 'code' => 'invalid_input', 'errors' => [text] }enddef nbsp = 0xA0.chr(Encoding::UTF_8)@@ -423,6 +436,28 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request doexpect(response).to have_http_status(:created)end+ # Every other example comments on a ticket the token owner created, where+ # the owner and the creator are the same person.+ it 'writes as the token owner on a ticket somebody else created' do+ creator = create(:user)+ foreign = ActsAsTenant.with_tenant(organisation) do+ create(:membership, user: creator, organisation: organisation, role: role, active: true)+ create(:ticket, project: project, organisation: organisation, creator: creator)+ end+ comment(ticket_id: foreign.id, format: 'plain', body: 'Hallo')+ expect(response).to have_http_status(:created)+ expect(body.dig('data', 'author', 'id')).to eq(user.id)+ expect(saved.user_id).to eq(user.id)+ ActsAsTenant.with_tenant(organisation) { expect(activity_for(saved).actor).to eq(user) }+ end++ it 'enqueues the embedding of the comment' do+ allow(EmbeddingProcessingJob).to receive(:perform_later)+ comment(format: 'plain', body: 'Hallo')+ expect(EmbeddingProcessingJob).to have_received(:perform_later)+ .with(record_class: Post, record_id: body.dig('data', 'id'), fragment_association: :fragments)+ end+it 'records a post_added activity with the excerpt of the text' docomment(format: 'plain', body: "Hallo <b>\nWelt")activity = activity_for(saved)diff --git a/spec/services/lalabase/tools/post_comment_failure_spec.rb b/spec/services/lalabase/tools/post_comment_failure_spec.rbindex 6a1c3e4..e2b1b54 100644--- a/spec/services/lalabase/tools/post_comment_failure_spec.rb+++ b/spec/services/lalabase/tools/post_comment_failure_spec.rb@@ -57,6 +57,30 @@ RSpec.describe Lalabase::Tools::PostComment, :ci_guard doexpect(result.error_code).to eq(:invalid_input)expect(result.error).to eq('body must not contain NUL bytes')end++ # An empty html body too: REST leaves it to the model after the write+ # check, MCP refuses it here, as it always did (app/services/CLAUDE.md).+ { 'format html' => { format: 'html' }, 'no format' => {} }.each do |label, format|+ it "refuses an empty html body for a missing ticket (#{label})" do+ result = described_class.execute(args: { ticket_id: SecureRandom.uuid, body: ' ' }.merge(format), context: context)+ expect(result.error_code).to eq(:invalid_input)+ expect(result.error).to eq('body is required')+ end+ end++ it 'refuses an empty html body for a ticket the caller may not read' do+ ProjectPermission.find_by!(user: user, project: project).update!(tickets_access: :no_access)+ result = described_class.execute(args: { ticket_id: ticket.id, body: ' ' }, context: context)+ expect(result.error_code).to eq(:invalid_input)+ expect(result.error).to eq('body is required')+ end+ end++ it 'enqueues the embedding of the comment, as the web form does' do+ allow(EmbeddingProcessingJob).to receive(:perform_later)+ result = execute+ expect(EmbeddingProcessingJob).to have_received(:perform_later)+ .with(record_class: Post, record_id: JSON.parse(result.content).fetch('id'), fragment_association: :fragments)end# The rule holds the reading of is_private (a JSON true or the stringdiff --git a/spec/services/posts/comment_spec.rb b/spec/services/posts/comment_spec.rbindex 2a1f3aa..8c37f63 100644--- a/spec/services/posts/comment_spec.rb+++ b/spec/services/posts/comment_spec.rb@@ -54,6 +54,13 @@ RSpec.describe Posts::Comment, :ci_guard do'invalid bytes in a binary body' => [{ format: 'plain', body: -> { "a#{255.chr}b".b } }, :body_invalid_utf8],'a plain body that does not transcode' => [{ format: 'plain', body: -> { "a#{0x82.chr}".force_encoding(Encoding::SHIFT_JIS) } },:body_invalid_utf8],+ # What a plain multipart part can bring past Rails' encoding check, which+ # refuses only bytes invalid in their own charset: a byte its charset has+ # no character for, and a charset Ruby cannot convert at all.+ 'a plain body with a byte its charset has no character for' =>+ [{ format: 'plain', body: -> { "a#{0x81.chr}b".force_encoding(Encoding::Windows_1252) } }, :body_invalid_utf8],+ 'a plain body in a charset Ruby cannot convert' =>+ [{ format: 'plain', body: -> { 'abc'.dup.force_encoding(Encoding::UTF_7) } }, :body_invalid_utf8],'a NUL byte in a plain body' => [{ format: 'plain', body: "a#{0.chr}b" }, :body_nul],'an empty plain body' => [{ format: 'plain', body: '' }, :body_empty],'a plain body of blank lines' => [{ format: 'plain', body: " \n\t\r\n " }, :body_empty],