cc-os/plugins/os-backlog/tests/test_helper.rb

140 lines
4.5 KiB
Ruby

require "minitest/autorun"
require_relative "../lib/backlog"
# Minimal stand-ins for the planka-api gem's error hierarchy, so tests never
# require (or need) the real gem installed — only the shape our code
# rescues against.
module Planka
class Error < StandardError; end
class ValidationError < Error; end
end
module BacklogTestHelpers
# A hand-rolled fake of the subset of Planka::Client's resource surface
# BoardEnsurer touches: projects, boards, lists, labels. In-memory only,
# no network. Mirrors the gem's real return shapes (string-keyed hashes).
class FakePlankaClient
Project = Struct.new(:id, :name, :type, :ownerProjectManagerId, :managers, keyword_init: true)
Board = Struct.new(:id, :project_id, :name, keyword_init: true)
List = Struct.new(:id, :board_id, :name, :type, keyword_init: true)
Label = Struct.new(:id, :board_id, :name, :color, keyword_init: true)
attr_reader :projects, :boards, :lists, :labels
def initialize(reject_colors: [])
@next_id = 0
@projects_store = []
@boards_store = []
@lists_store = []
@labels_store = []
@projects = ProjectsResource.new(self)
@boards = BoardsResource.new(self)
@lists = ListsResource.new(self)
@labels = LabelsResource.new(self, reject_colors: reject_colors)
end
def next_id = (@next_id += 1).to_s
attr_reader :projects_store, :boards_store, :lists_store, :labels_store
class ProjectsResource
def initialize(client) = @client = client
def list = @client.projects_store.map { |p| stringify(p) }
def create(attrs)
project = FakePlankaClient::Project.new(
id: @client.next_id, name: attrs[:name], type: attrs[:type],
ownerProjectManagerId: @client.next_id, managers: []
)
@client.projects_store << project
stringify(project)
end
def update(id, attrs)
project = @client.projects_store.find { |p| p.id == id }
project.ownerProjectManagerId = attrs[:ownerProjectManagerId] if attrs.key?(:ownerProjectManagerId)
stringify(project)
end
def add_manager(project_id, user_id)
project = @client.projects_store.find { |p| p.id == project_id }
project.managers << user_id
{ "id" => @client.next_id, "projectId" => project_id, "userId" => user_id }
end
private
def stringify(p) = { "id" => p.id, "name" => p.name, "type" => p.type }
end
class BoardsResource
def initialize(client) = @client = client
def list(project_id)
@client.boards_store.select { |b| b.project_id == project_id }.map { |b| stringify(b) }
end
def create(project_id, attrs)
board = FakePlankaClient::Board.new(id: @client.next_id, project_id: project_id, name: attrs[:name])
@client.boards_store << board
stringify(board)
end
def update(id, attrs)
board = @client.boards_store.find { |b| b.id == id }
board.name = attrs[:name] if attrs.key?(:name)
stringify(board)
end
private
def stringify(b) = { "id" => b.id, "projectId" => b.project_id, "name" => b.name }
end
class ListsResource
def initialize(client) = @client = client
def list(board_id)
@client.lists_store.select { |l| l.board_id == board_id }.map { |l| stringify(l) }
end
def create(board_id, attrs)
raise ArgumentError, "list create requires type" unless attrs[:type]
list = FakePlankaClient::List.new(id: @client.next_id, board_id: board_id, name: attrs[:name], type: attrs[:type])
@client.lists_store << list
stringify(list)
end
private
def stringify(l) = { "id" => l.id, "boardId" => l.board_id, "name" => l.name, "type" => l.type }
end
class LabelsResource
def initialize(client, reject_colors: [])
@client = client
@reject_colors = reject_colors
end
def list(board_id)
@client.labels_store.select { |l| l.board_id == board_id }.map { |l| stringify(l) }
end
def create(board_id, attrs)
raise Planka::ValidationError, "color #{attrs[:color]} rejected" if @reject_colors.include?(attrs[:color])
label = FakePlankaClient::Label.new(id: @client.next_id, board_id: board_id, name: attrs[:name], color: attrs[:color])
@client.labels_store << label
stringify(label)
end
private
def stringify(l) = { "id" => l.id, "boardId" => l.board_id, "name" => l.name, "color" => l.color }
end
end
end