fix: make admin command parsing more robust

There was a bug in how we parsed admin commands, apparently we never
tested if we could parse `!backlog X` or unknown admin commands.

This commit also make updates to the backlog command construction to
make sure that we don't try to access messages in the backlog that
don't exist.
This commit is contained in:
Jacob Jonsson 2026-03-11 00:48:02 +01:00
parent d237ba9e8a
commit 4e11cc9ea1
Signed by: Jassob
GPG key ID: 7E30B9B047F7202E
4 changed files with 100 additions and 40 deletions

View file

@ -120,28 +120,38 @@ pub const BotAdapter = struct {
}
};
// test "substitute" {
// var bot_adapter = try BotAdapter.init(std.testing.allocator);
// defer bot_adapter.deinit();
// const prefix = zircon.Prefix{ .nick = "jassob", .user = "jassob", .host = "localhost" };
// const msg = zircon.Message{
// .PRIVMSG = .{
// .prefix = prefix,
// .targets = "#test",
// .text = "hello world",
// },
// };
// if (bot_adapter.callback(msg)) |_| {
// @panic("unexpected response");
// }
// const cmd_msg = zircon.Message{
// .PRIVMSG = .{
// .prefix = prefix,
// .targets = "#test",
// .text = "s/world/zig/",
// },
// };
// const response = bot_adapter.callback(cmd_msg);
// try std.testing.expect(response != null);
// try std.testing.expectEqualStrings("jassob: \"hello zig\"", response.?.PRIVMSG.text);
// }
test "substitute" {
var bot_adapter = try BotAdapter.init(std.testing.allocator);
defer bot_adapter.deinit();
const prefix = zircon.Prefix{ .nick = "jassob", .user = "jassob", .host = "localhost" };
const msg = zircon.Message{
.PRIVMSG = .{
.prefix = prefix,
.targets = "#test",
.text = "hello world",
},
};
if (bot_adapter.callback(msg)) |_| {
@panic("unexpected response");
}
const cmd_msg = zircon.Message{
.PRIVMSG = .{
.prefix = prefix,
.targets = "#test",
.text = "s/world/zig/",
},
};
const response = bot_adapter.callback(cmd_msg);
try std.testing.expect(response != null);
try std.testing.expectEqualStrings("jassob: \"hello zig\"", response.?.PRIVMSG.text);
}
test "get empty backlog message" {
var bot_adapter = try BotAdapter.init(std.testing.allocator);
defer bot_adapter.deinit();
const prefix = zircon.Prefix{ .nick = "jassob", .user = "jassob", .host = "localhost" };
const msg = zircon.Message{
.PRIVMSG = .{ .prefix = prefix, .targets = "#eru-admin", .text = "!backlog 0" },
};
try std.testing.expectEqualDeep("no matching message", bot_adapter.callback(msg).?.PRIVMSG.text);
}