|
1 | 1 | // Irc tests cover client plugin behavior. |
2 | 2 | import { describe, expect, it } from "vitest"; |
3 | | -import { buildIrcNickServCommands } from "./client.js"; |
| 3 | +import { buildFallbackNick, buildIrcNickServCommands } from "./client.js"; |
4 | 4 | |
5 | 5 | describe("irc client nickserv", () => { |
6 | 6 | it("builds IDENTIFY command when password is set", () => { |
@@ -42,3 +42,38 @@ describe("irc client nickserv", () => {
|
42 | 42 | ).toEqual(["PRIVMSG NickServ :IDENTIFY secret JOIN #bad"]); |
43 | 43 | }); |
44 | 44 | }); |
| 45 | + |
| 46 | +describe("irc client fallback nick", () => { |
| 47 | +it("produces unique fallback nicks across sequential calls", () => { |
| 48 | +const first = buildFallbackNick("bot"); |
| 49 | +const second = buildFallbackNick("bot"); |
| 50 | +const third = buildFallbackNick("bot"); |
| 51 | +// First call gets suffix _ (seq=1), subsequent calls get _2, _3, ... |
| 52 | +expect(first).toBe("bot_"); |
| 53 | +expect(second).toMatch(/^bot_\d+$/); |
| 54 | +expect(third).toMatch(/^bot_\d+$/); |
| 55 | +expect(new Set([first, second, third]).size).toBe(3); |
| 56 | +}); |
| 57 | + |
| 58 | +it("sanitizes whitespace and special characters in nick", () => { |
| 59 | +const nick = buildFallbackNick("my bot!"); |
| 60 | +expect(nick).toMatch(/^mybot_\d*$/); |
| 61 | +}); |
| 62 | + |
| 63 | +it("falls back to openclaw when nick consists entirely of special characters", () => { |
| 64 | +const nick = buildFallbackNick("!!!"); |
| 65 | +expect(nick).toMatch(/^openclaw_\d*$/); |
| 66 | +}); |
| 67 | + |
| 68 | +it("falls back to openclaw when nick is empty after sanitization", () => { |
| 69 | +const nick = buildFallbackNick(""); |
| 70 | +expect(nick).toMatch(/^openclaw_\d*$/); |
| 71 | +}); |
| 72 | + |
| 73 | +it("truncates long nicks to max 30 chars", () => { |
| 74 | +const longNick = "a".repeat(50); |
| 75 | +const nick = buildFallbackNick(longNick); |
| 76 | +expect(nick.length).toBeLessThanOrEqual(30); |
| 77 | +expect(nick).toMatch(/^a+_\d*$/); |
| 78 | +}); |
| 79 | +}); |