make testing quieter, match expectations

This commit is contained in:
Cadey Ratio 2019-12-26 02:33:13 +00:00
parent b00c7c10d7
commit aa777a6ab6
4 changed files with 86 additions and 26 deletions

View File

@ -3,13 +3,13 @@ socket = require "socket"
test = require "ketracel.test" test = require "ketracel.test"
Server = require("ketracel.server").Server Server = require("ketracel.server").Server
uuid = require "uuid" uuid = require "uuid"
ln = require "ln"
describe "ngircd protocol support", -> describe "ngircd protocol support", ->
local server local server
local client local client
setup -> setup ->
print "got here"
file = io.popen "ngircd -n -f ./spec/ngircd.conf", "r" file = io.popen "ngircd -n -f ./spec/ngircd.conf", "r"
server = Server server = Server
server: "127.0.0.1" server: "127.0.0.1"
@ -17,7 +17,7 @@ describe "ngircd protocol support", ->
spass: "hunter2" spass: "hunter2"
sreal: "Ketracel" sreal: "Ketracel"
nicklen: "31" nicklen: "31"
debug: true debug: os.getenv "DEBUG"
client = test.Client "test_user" client = test.Client "test_user"
teardown -> teardown ->
@ -29,15 +29,59 @@ describe "ngircd protocol support", ->
data = \read "*all" data = \read "*all"
assert.truthy string.find data, "ngircd" assert.truthy string.find data, "ngircd"
it "registers", -> it "sees end of burst", ->
server\wait_for "376" server\wait_for "376"
it "can use the Client class", -> it "can use the Client class", ->
client\wait_for "005" client\wait_for "005"
it "sees the client", -> it "sees the client", ->
server\wait_for "NICK" server\wait_for "NICK", (source, user) ->
user.nick == "test_user"
local channame
it "sees the client join a new channel", -> it "sees the client join a new channel", ->
client.irc\JOIN "#" .. uuid() channame = uuid()
server\wait_for "JOIN" client.irc\JOIN "#" .. channame
server\wait_for "JOIN", (who, chan) ->
chan.name == "#" .. channame
client\wait_for "JOIN", (sender, channel) ->
channel\sub(2) == channame
it "sees the client part the channel", ->
client.irc\PART "#" .. channame
server\wait_for "PART", (who, chan) ->
chan.name == "#" .. channame
it "sees nickchanges", ->
client.irc\send_raw "NICK newnick"
server\wait_for "NICKCHG", (sender, clinfo) ->
clinfo.nick == "newnick"
it "sees quits", ->
cli = test.Client "quitter"
cli\wait_for "005"
cli.irc\QUIT "bye"
server\wait_for "QUIT", (sender, msg) ->
sender == "quitter"
it "sees a privmsg", ->
client.irc\JOIN "#ketracel"
client.irc\PRIVMSG "#ketracel", "hi"
server\wait_for "PRIVMSG", (sender, params) ->
params[1] == "#ketracel" and params[2] == "hi"
describe "ketracel bot", ->
it "has a working VHOST command", ->
client.irc\PRIVMSG "Ketracel", "VHOST my.cool.vhost"
server\wait_for "PRIVMSG", (sender, params) ->
params[1] == "Ketracel" and params[2] == "VHOST my.cool.vhost"
client\wait_for "396"
client\wait_for "NOTICE", (sender, origin, message, pm) ->
message\find "my.cool.vhost"
it "replies to unknown commands", ->
client.irc\PRIVMSG "KETRACEL", uuid()
server\wait_for "PRIVMSG"
client\wait_for "NOTICE"

View File

@ -2,6 +2,7 @@ irce = require "irce"
util = require "irce.util" util = require "irce.util"
stringx = require "pl.stringx" stringx = require "pl.stringx"
moon = require "moon" moon = require "moon"
ln = require "ln"
{ {
init: (state) => init: (state) =>
@ -99,15 +100,18 @@ moon = require "moon"
state.channels[chan] = {name: chan, mode: "", members: {}, topic: ""} state.channels[chan] = {name: chan, mode: "", members: {}, topic: ""}
state.channels[chan].members[nick] = mode state.channels[chan].members[nick] = mode
state.channels[chan] sender[1], state.channels[chan]
["PART"]: (state, sender, params) => ["PART"]: (state, sender, params) =>
chan = params[1] chan = params[1]
nick = string.lower sender[1] nick = string.lower sender[1]
cinfo = state.channels[chan]
state.channels[chan].members[nick] = nil state.channels[chan].members[nick] = nil
if #state.channels[chan].members == 0 if #state.channels[chan].members == 0
state.channels[chan] = nil state.channels[chan] = nil
sender[1], cinfo
["QUIT"]: (state, sender, params) => ["QUIT"]: (state, sender, params) =>
nick = string.lower sender[1] nick = string.lower sender[1]
state.clients[nick] = nil state.clients[nick] = nil
@ -115,6 +119,8 @@ moon = require "moon"
for k, v in pairs state.channels for k, v in pairs state.channels
v.members[nick] = nil v.members[nick] = nil
sender[1], params[1]
["NICK"]: (state, sender, params) => ["NICK"]: (state, sender, params) =>
if #params == 1 if #params == 1
oldnick = sender[1] oldnick = sender[1]
@ -132,7 +138,8 @@ moon = require "moon"
v.members[nick] = v.members[oldnicksmall] v.members[nick] = v.members[oldnicksmall]
v.members[oldnicksmall] = nil v.members[oldnicksmall] = nil
return sender, state.clients[nick] @handle "NICKCHG", sender, state.clients[nick]
return
nick = params[1] nick = params[1]
user = params[3] user = params[3]
@ -196,6 +203,7 @@ moon = require "moon"
state.channels[name] = state.channels[name] =
:name, :mode, :topic, :key, :limit, members: {} :name, :mode, :topic, :key, :limit, members: {}
state.channels[name]
["PRIVMSG"]: (state, sender, params) => ["PRIVMSG"]: (state, sender, params) =>
if params[2] == "?state" if params[2] == "?state"

View File

@ -5,14 +5,14 @@ socket = require "socket"
class Server class Server
new: (config) => new: (config) =>
@config = config @config = config
socket = socket.tcp! sock = socket.tcp!
irc = irce.new! irc = irce.new!
irc\load_module require "irce.modules.ngircd" irc\load_module require "irce.modules.ngircd"
irc\load_module require "ketracel.bots.ketracel" irc\load_module require "ketracel.bots.ketracel"
irc\set_send_func (message) => irc\set_send_func (message) =>
socket\send message sock\send message
if config.debug if config.debug
ln.log {"msg": "debug enabled"}, config ln.log {"msg": "debug enabled"}, config
@ -22,21 +22,26 @@ class Server
ln.log msg: "Ketracel loaded", irce: irce._VERSION, lua: _VERSION ln.log msg: "Ketracel loaded", irce: irce._VERSION, lua: _VERSION
assert socket\connect config.server, 6667 assert sock\connect config.server, 6667
assert irc\REGISTER config.spass, "ketracel", "0.0.1", config.sname, config.sreal, config.nicklen assert irc\REGISTER config.spass, "ketracel", "0.0.1", config.sname, config.sreal, config.nicklen
@irc = irc @irc = irc
@socket = socket @socket = sock
wait_for: (event) => wait_for: (event, checker) =>
running = true running = true
@irc\set_callback event, => @irc\set_callback event, (...) =>
if checker and not checker(...)
return
print "! got event " .. event if os.getenv "DEBUG"
running = false running = false
while running while running
@irc\process @socket\receive! @irc\process @socket\receive!
@irc\clear_callback event
run: => run: =>
running = true running = true

View File

@ -9,32 +9,35 @@ get_client_modules = (irc) ->
class Client class Client
new: (nick) => new: (nick) =>
@nick = nick @nick = nick
socket = socket.tcp! sock = socket.tcp!
irc = irce.new! irc = irce.new!
get_client_modules irc get_client_modules irc
irc\set_send_func (message) => irc\set_send_func (message) =>
print string.format "[client %s] > %s", nick, message print string.format "[client %s] > %s", nick, message if os.getenv "DEBUG"
socket\send message sock\send message
socket\connect "127.0.0.1", 6667 sock\connect "127.0.0.1", 6667
irc\NICK nick irc\NICK nick
irc\USER nick, nick irc\USER nick, nick
@socket = socket @socket = sock
@irc = irc @irc = irc
wait_for: (event) => wait_for: (event, checker) =>
running = true running = true
nick = @nick nick = @nick
@irc\set_callback event, => @irc\set_callback event, (...) =>
print string.format "[client %s] ! got event %s", nick, event if checker and not checker ...
return
print string.format "[client %s] ! got event %s", nick, event if os.getenv "DEBUG"
running = false running = false
while running while running
msg = @socket\receive! msg = assert @socket\receive!
print string.format "[client %s] < %s", nick, msg print string.format "[client %s] < %s", nick, msg if os.getenv "DEBUG"
@irc\process msg @irc\process msg
@irc\clear_callback event
quit: => quit: =>
@irc\QUIT "bye" @irc\QUIT "bye"
@socket\close! @socket\close!