main
1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Del::Message do
6 subject { described_class.new(text, robot: robot, source: source) }
7 let(:robot) { instance_double(Del::Robot) }
8 let(:source) { instance_double(Del::Source) }
9 let(:text) { SecureRandom.hex(16) }
10
11 describe '#reply' do
12 before { allow(source).to receive(:reply) }
13
14 it 'delegates to the source to reply' do
15 subject.reply('hello')
16 expect(source).to have_received(:reply).with(robot, 'hello')
17 end
18 end
19
20 describe '#execute_shell' do
21 before { allow(source).to receive(:reply) }
22
23 it 'executes the command' do
24 result = subject.execute_shell(['ls', '-al'])
25 expect(result).to be_truthy
26 end
27
28 it 'returns false when the shell command fails' do
29 expect(subject.execute_shell(%w[exit 1])).to be_falsey
30 end
31
32 it 'replies with the stdout content' do
33 subject.execute_shell(%w[echo hello])
34 expect(source).to have_received(:reply).with(robot, "/code hello\n")
35 end
36
37 it 'yields each line to stdout' do
38 @called = false
39 subject.execute_shell(%w[echo hello]) do |line|
40 @called = true
41 expect(line).to eql("hello\n")
42 end
43 expect(@called).to be(true)
44 end
45
46 it 'replies with the stderr content' do
47 subject.execute_shell(['>&2', 'echo', 'hello'])
48 expect(source).to have_received(:reply).with(robot, "/code hello\n")
49 end
50
51 it 'yields each line to stderr' do
52 @called = false
53 subject.execute_shell(['>&2', 'echo', 'hello']) do |line|
54 @called = true
55 expect(line).to eql("hello\n")
56 end
57 expect(@called).to be(true)
58 end
59 end
60
61 describe '#to_s' do
62 specify { expect(subject.to_s).to include(source.to_s) }
63 specify { expect(subject.to_s).to include(text) }
64 end
65end