| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | |
| 5 | RSpec.describe CommentService do |
| 6 | let(:owner) { User.create!(smbcloud_id: 8001, email: "owner@example.com", username: "owner") } |
| 7 | let(:author) { User.create!(smbcloud_id: 8002, email: "author@example.com", username: "author") } |
| 8 | let(:repo) do |
| 9 | owner.repositories.create!( |
| 10 | name: "app", kind: "code", default_branch: "main", |
| 11 | disk_path: GitRepositoryService.repo_path("owner", "app") |
| 12 | ) |
| 13 | end |
| 14 | |
| 15 | it "comments on an issue by number" do |
| 16 | issue = repo.issues.create!(user: owner, number: 1, title: "Bug") |
| 17 | comment = described_class.create(repository: repo, author: author, number: 1, body: "On it") |
| 18 | |
| 19 | expect(comment).to be_persisted |
| 20 | expect(comment.commentable).to eq(issue) |
| 21 | expect(comment.author).to eq(author) |
| 22 | end |
| 23 | |
| 24 | it "comments on a pull request that shares the number space" do |
| 25 | pr = repo.pull_requests.create!(user: owner, number: 2, title: "PR", head_ref: "feature", base_ref: "main") |
| 26 | comment = described_class.create(repository: repo, author: author, number: 2, body: "LGTM") |
| 27 | |
| 28 | expect(comment.commentable).to eq(pr) |
| 29 | end |
| 30 | |
| 31 | it "raises NotFound when no issue or PR has that number" do |
| 32 | expect do |
| 33 | described_class.create(repository: repo, author: author, number: 99, body: "?") |
| 34 | end.to raise_error(CommentService::NotFound, /No issue or pull request #99/) |
| 35 | end |
| 36 | |
| 37 | it "rejects a blank body via model validation" do |
| 38 | repo.issues.create!(user: owner, number: 1, title: "Bug") |
| 39 | expect do |
| 40 | described_class.create(repository: repo, author: author, number: 1, body: "") |
| 41 | end.to raise_error(ActiveRecord::RecordInvalid) |
| 42 | end |
| 43 | end |