git » sdk » commit e8ce5ff

Add tests

author Stephen Paul Weber
2026-10-28 20:49:11 UTC
committer Stephen Paul Weber
2026-11-09 14:51:19 UTC
parent 4a22731cbb67777a9d088175ceb8c4e0ecb968ad

Add tests

test/TestChat.hx +65 -0
test/TestClient.hx +316 -0
test/TestMucSettingsCommand.hx +73 -3

diff --git a/test/TestChat.hx b/test/TestChat.hx
index 71269cb..444a2ef 100644
--- a/test/TestChat.hx
+++ b/test/TestChat.hx
@@ -8,10 +8,12 @@ import borogove.Stanza;
 import borogove.JID;
 import borogove.persistence.Dummy;
 import borogove.CapsRepo;
+import borogove.Chat;
 import borogove.Chat.Channel;
 import borogove.Chat.AvailableChat;
 import borogove.Caps.Identity;
 import borogove.Member;
+import borogove.Presence;
 import borogove.Role;
 import thenshim.Promise;
 using Lambda;
@@ -651,6 +653,69 @@ class TestChat extends utest.Test {
 		Assert.isFalse(channel.canSend());
 	}
 
+	public function testCreationRequires201(async: Async) {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		final channel = new Channel(client, client.stream, persistence, "room@example.com", Creating);
+		var sentUnavailable = false;
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name == "presence" && stanza.attr.get("type") == "unavailable") sentUnavailable = true;
+			return EventUnhandled;
+		});
+
+		Assert.isFalse(channel.canSend());
+
+		channel.join().then(_ -> {
+			final presence: Presence = cast Stanza.parse('<presence xmlns="jabber:client" from="room@example.com/test" to="test@example.com"><x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="member" role="participant"/><status code="110"/></x></presence>');
+			channel.setPresence("test", presence, false);
+
+			Assert.equals("conflict", channel.joinFailed?.getError()?.condition);
+			Assert.equals(Closed, channel.uiState);
+			Assert.isTrue(sentUnavailable);
+			async.done();
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreationErrorPresenceClosesChannel(async: Async) {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		final channel = new Channel(client, client.stream, persistence, "room@example.com", Creating);
+		client.chats.push(channel);
+		client.chatsIndex[channel.chatId] = channel;
+
+		channel.join().then(_ -> {
+			client.stream.onStanza(Stanza.parse('<presence xmlns="jabber:client" from="room@example.com/test" to="test@example.com" type="error"><error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>'));
+
+			Assert.equals("forbidden", channel.joinFailed?.getError()?.condition);
+			Assert.equals(Closed, channel.uiState);
+			async.done();
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testSelfPingNoopWhileCreating() {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		final channel = new Channel(client, client.stream, persistence, "room@example.com", Creating);
+		var sentPing = false;
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name == "iq" && stanza.getChild("ping", "urn:xmpp:ping") != null) sentPing = true;
+			return EventUnhandled;
+		});
+
+		channel.selfPing(true);
+		Assert.isFalse(sentPing);
+	}
+
 	public function testJoinFailure() {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
diff --git a/test/TestClient.hx b/test/TestClient.hx
index 2a71b9f..27fe16d 100644
--- a/test/TestClient.hx
+++ b/test/TestClient.hx
@@ -5,6 +5,7 @@ import utest.Assert;
 import utest.Async;
 
 import borogove.Chat;
+import borogove.Chat.Channel;
 import borogove.ChatMessage;
 import borogove.ChatMessageBuilder;
 import borogove.Client;
@@ -15,6 +16,8 @@ import borogove.Member;
 import borogove.MemberUpdate;
 import borogove.Message;
 import borogove.ModerationAction;
+import borogove.MucSettingsCommand;
+import borogove.Presence;
 import borogove.Role;
 import borogove.Stanza;
 import borogove.Status;
@@ -30,6 +33,24 @@ using Lambda;
 
 @:access(borogove)
 class TestClient extends utest.Test {
+	private function errorStanza(condition: String, type: String = "cancel"): Stanza {
+		return new Stanza("presence", { type: "error" })
+			.tag("error", { type: type })
+				.tag(condition, { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" }).up()
+			.up();
+	}
+
+	private function discardChannel(client: Client, channel: Channel): Void {
+		channel.setJoinFailed(errorStanza("internal-server-error"));
+		client.chats.remove(channel);
+		client.chatsIndex.remove(channel.chatId);
+	}
+
+	private function submittedField(stanza: Stanza, name: String): Null<String> {
+		final x = stanza.getChild("query", "http://jabber.org/protocol/muc#owner")?.getChild("x", "jabber:x:data");
+		return x?.allTags("field").find(field -> field.attr.get("var") == name)?.getChildText("value");
+	}
+
 	public function testPresenceAppliedBeforeCapsDiscovery() {
 		final client = new Client("test@example.com", new Dummy());
 		final channel = new borogove.Chat.Channel(client, client.stream, client.persistence, "room@example.com");
@@ -1569,6 +1590,287 @@ class TestClient extends utest.Test {
 		});
 	}
 
+	public function testCreateChannelDerivesLocalpartFromName(async: Async) {
+		final client = new Client("test@example.com", new Dummy());
+
+		client.createChannel("  Design Team & Chat!  ", "   ", "groups.example.com").then(channel -> {
+			Assert.equals("design-team-chat@groups.example.com", channel.chatId);
+			discardChannel(client, channel);
+			async.done();
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreateChannelFallsBackToUuid(async: Async) {
+		final client = new Client("test@example.com", new Dummy());
+
+		client.createChannel(null, "   ", "groups.example.com").then(channel -> {
+			final parsed = JID.parse(channel.chatId);
+			Assert.notNull(parsed.node);
+			Assert.isFalse(parsed.node == "");
+			Assert.equals("groups.example.com", parsed.domain);
+			discardChannel(client, channel);
+			async.done();
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreateChannelUsesPersistedService(async: Async) {
+		final persistence = new ChannelCreationMockPersistence();
+		persistence.services = [{
+			serviceId: "groups.example.com",
+			name: "Groups",
+			node: null,
+			caps: new borogove.Caps("", [], ["http://jabber.org/protocol/muc"], []),
+		}];
+		final client = new Client("test@example.com", persistence);
+
+		client.createChannel(null, "design-team").then(channel -> {
+			Assert.equals("design-team@groups.example.com", channel.chatId);
+			discardChannel(client, channel);
+			async.done();
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreateChannelWithoutServiceRejects(async: Async) {
+		final client = new Client("test@example.com", new Dummy());
+
+		client.createChannel(null, null).then(_ -> {
+			Assert.fail("Expected createChannel to reject");
+			return null;
+		}, err -> {
+			Assert.notNull(err);
+			async.done();
+			return null;
+		});
+	}
+
+	public function testCreateChannelRejectsExistingChat(async: Async) {
+		final client = new Client("test@example.com", new Dummy());
+
+		client.createChannel(null, "design-team", "groups.example.com").then(channel -> {
+			client.createChannel(null, "design-team", "groups.example.com").then(_ -> {
+				Assert.fail("Expected second createChannel to reject");
+				return null;
+			}, err -> {
+				Assert.notNull(err);
+				discardChannel(client, channel);
+				async.done();
+				return null;
+			});
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreateChannelInstantConfiguresAutomatically(async: Async) {
+		final persistence = new ChannelCreationMockPersistence();
+		final client = new Client("test@example.com", persistence);
+
+		var created: Null<Channel> = null;
+		var sentPresence: Null<Stanza> = null;
+		var submitted: Null<Stanza> = null;
+		var ownerGets = 0;
+		var ownerSets = 0;
+		var done = false;
+
+		client.on("chats/update", (chats: Array<Chat>) -> {
+			final channel = created;
+			if (done || channel == null || chats.indexOf(channel) < 0 || channel.uiState != Open) return EventHandled;
+
+			done = true;
+			Assert.equals(1, ownerGets);
+			Assert.equals(1, ownerSets);
+			Assert.notNull(submitted);
+			Assert.equals("Design Team", submittedField(submitted, "muc#roomconfig_roomname"));
+			Assert.equals("0", submittedField(submitted, "muc#roomconfig_membersonly"));
+			Assert.equals("1", submittedField(submitted, "muc#roomconfig_persistentroom"));
+			Assert.equals("anyone", submittedField(submitted, "muc#roomconfig_whois"));
+			Assert.equals("1", submittedField(submitted, "muc#roomconfig_allowinvites"));
+			Assert.equals(Open, channel.uiState);
+			Assert.isTrue(channel.canSend());
+			Assert.isTrue(persistence.storedChats.indexOf(channel) >= 0);
+			async.done();
+			return EventHandled;
+		});
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name == "presence") {
+				sentPresence = stanza;
+				return EventHandled;
+			}
+
+			if (stanza.name == "iq" && stanza.attr.get("type") == "get") {
+				final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+				if (query != null) {
+					ownerGets++;
+					final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "result", id: stanza.attr.get("id"), from: "design-team@groups.example.com", to: "test@example.com" })
+						.tag("query", { xmlns: "http://jabber.org/protocol/muc#owner" })
+							.tag("x", { xmlns: "jabber:x:data", type: "form" })
+								.tag("field", { "var": "muc#roomconfig_roomname" })
+									.textTag("value", "Server Default")
+								.up()
+								.tag("field", { "var": "muc#roomconfig_membersonly", type: "boolean" })
+									.textTag("value", "1")
+								.up()
+							.up()
+						.up();
+
+					client.stream.onStanza(responseStanza);
+					return EventHandled;
+				}
+			} else if (stanza.name == "iq" && stanza.attr.get("type") == "set") {
+				final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+				if (query != null) {
+					ownerSets++;
+					submitted = stanza;
+					final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "result", id: stanza.attr.get("id"), from: "design-team@groups.example.com", to: "test@example.com" });
+					client.stream.onStanza(responseStanza);
+					return EventHandled;
+				}
+			}
+
+			return EventUnhandled;
+		});
+
+		client.createChannelInstant("Design Team", false, "design-team", "groups.example.com").then(channel -> {
+			created = channel;
+
+			Assert.equals("design-team@groups.example.com", channel.chatId);
+			Assert.equals("Design Team", channel.getDisplayName());
+			Assert.equals(Creating, channel.uiState);
+			Assert.isFalse(channel.canSend());
+			Assert.equals(channel, client.getChat(channel.chatId));
+			Assert.isTrue(client.getChats().indexOf(channel) >= 0);
+			Assert.isFalse(persistence.storedChats.indexOf(channel) >= 0);
+			Assert.notNull(channel.creationFormSubmission);
+			Assert.equals("Design Team", channel.creationFormSubmission.get("muc#roomconfig_roomname")[0]);
+			Assert.equals("0", channel.creationFormSubmission.get("muc#roomconfig_membersonly")[0]);
+			Assert.notNull(sentPresence);
+			Assert.equals("design-team@groups.example.com/test", sentPresence.attr.get("to"));
+			Assert.notNull(sentPresence.getChild("x", "http://jabber.org/protocol/muc"));
+
+			final presence: Presence = cast Stanza.parse('<presence xmlns="jabber:client" from="design-team@groups.example.com/test" to="test@example.com"><x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="owner" role="moderator"/><status code="110"/><status code="201"/></x></presence>');
+			client.stream.onStanza(presence);
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
+	public function testCreateChannelDefersConfig(async: Async) {
+		final persistence = new ChannelCreationMockPersistence();
+		final client = new Client("test@example.com", persistence);
+
+		var submitted: Null<Stanza> = null;
+		var ownerGets = 0;
+		var ownerSets = 0;
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name == "iq" && stanza.attr.get("type") == "get") {
+				final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+				if (query != null) {
+					ownerGets++;
+					final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "result", id: stanza.attr.get("id"), from: "design-team@groups.example.com", to: "test@example.com" })
+						.tag("query", { xmlns: "http://jabber.org/protocol/muc#owner" })
+							.tag("x", { xmlns: "jabber:x:data", type: "form" })
+								.tag("field", { "var": "muc#roomconfig_roomname" })
+									.textTag("value", "Server Default")
+								.up()
+							.up()
+						.up();
+
+					client.stream.onStanza(responseStanza);
+					return EventHandled;
+				}
+			} else if (stanza.name == "iq" && stanza.attr.get("type") == "set") {
+				final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+				if (query != null) {
+					ownerSets++;
+					submitted = stanza;
+					final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "result", id: stanza.attr.get("id"), from: "design-team@groups.example.com", to: "test@example.com" });
+					client.stream.onStanza(responseStanza);
+					return EventHandled;
+				}
+			}
+			return EventUnhandled;
+		});
+
+		client.createChannel("Design Team", "design-team", "groups.example.com").then(channel -> {
+			Assert.equals("design-team@groups.example.com", channel.chatId);
+			Assert.equals(Creating, channel.uiState);
+			Assert.isFalse(channel.canSend());
+			Assert.isNull(channel.creationFormSubmission);
+			Assert.notNull(channel.creationFormPrefill);
+			Assert.equals("Design Team", channel.creationFormPrefill.get("muc#roomconfig_roomname")[0]);
+
+			final presence: Presence = cast Stanza.parse('<presence xmlns="jabber:client" from="design-team@groups.example.com/test" to="test@example.com"><x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="owner" role="moderator"/><status code="110"/><status code="201"/></x></presence>');
+			client.stream.onStanza(presence);
+
+			Assert.isTrue(channel.isJoined);
+			Assert.equals(Creating, channel.uiState);
+			Assert.isFalse(channel.canSend());
+			Assert.equals(0, ownerGets);
+
+			channel.commands().then(cmds -> {
+				final settings = cmds.filter(cmd -> Std.isOfType(cmd, MucSettingsCommand))[0];
+				settings.execute().then(session -> {
+					Assert.equals("executing", session.status);
+					final nameItem = session.forms[0].items().find(item -> item.field?.name == "muc#roomconfig_roomname");
+					Assert.notNull(nameItem);
+					Assert.equals("Design Team", nameItem.field.value[0]);
+
+					#if js
+					final data = {};
+					#else
+					final data = new borogove.Form.FormSubmitBuilder();
+					#end
+
+					session.execute(null, data, 0).then(nextSession -> {
+						Assert.equals("completed", nextSession.status);
+						Assert.equals(1, ownerGets);
+						Assert.equals(1, ownerSets);
+						Assert.notNull(submitted);
+						Assert.equals("Design Team", submittedField(submitted, "muc#roomconfig_roomname"));
+						Assert.equals(Open, channel.uiState);
+						Assert.isTrue(channel.canSend());
+						Assert.isTrue(persistence.storedChats.indexOf(channel) >= 0);
+						async.done();
+						return null;
+					}, err -> {
+						Assert.fail(err);
+						return null;
+					});
+					return null;
+				}, err -> {
+					Assert.fail(err);
+					return null;
+				});
+				return null;
+			}, err -> {
+				Assert.fail(err);
+				return null;
+			});
+			return null;
+		}, err -> {
+			Assert.fail(err);
+			return null;
+		});
+	}
+
 #if js
 	public function testPreferOMEMO() {
 		final persistence = new Dummy();
@@ -1702,3 +2004,17 @@ class RestoreChatsMockPersistence extends Dummy {
 		}
 	}
 }
+
+@:access(borogove)
+class ChannelCreationMockPersistence extends Dummy {
+	public var storedChats: Array<Chat> = [];
+	public var services: Array<{serviceId: String, name: Null<String>, node: Null<String>, caps: borogove.Caps}> = [];
+
+	override public function storeChats(accountId: String, chats: Array<Chat>) {
+		storedChats = storedChats.concat(chats);
+	}
+
+	override public function findServicesWithFeature(accountId: String, feature: String): Promise<Array<{serviceId: String, name: Null<String>, node: Null<String>, caps: borogove.Caps}>> {
+		return Promise.resolve(services);
+	}
+}
diff --git a/test/TestMucSettingsCommand.hx b/test/TestMucSettingsCommand.hx
index a3d9463..f2e3a7b 100644
--- a/test/TestMucSettingsCommand.hx
+++ b/test/TestMucSettingsCommand.hx
@@ -14,7 +14,8 @@ class TestMucSettingsCommand extends utest.Test {
 	public function testExecuteSuccess(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
-		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"));
+		var completed = 0;
+		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"), () -> completed++);
 
 		client.stream.on("sendStanza", (stanza: Stanza) -> {
 			if (stanza.name == "iq" && stanza.attr.get("type") == "get") {
@@ -41,6 +42,7 @@ class TestMucSettingsCommand extends utest.Test {
 			Assert.equals("executing", session.status);
 			Assert.equals(1, session.forms.length);
 			Assert.notNull(session.forms[0]);
+			Assert.equals(0, completed);
 
 			client.stream.on("sendStanza", (stanza: Stanza) -> {
 				if (stanza.name == "iq" && stanza.attr.get("type") == "set") {
@@ -66,6 +68,70 @@ class TestMucSettingsCommand extends utest.Test {
 			session.execute(null, data, 0).then(nextSession -> {
 				Assert.equals("completed", nextSession.status);
 				Assert.equals(0, nextSession.forms.length);
+				Assert.equals(1, completed);
+				async.done();
+			}, err -> {
+				Assert.fail(err);
+			});
+		}, err -> {
+			Assert.fail(err);
+		});
+	}
+
+	public function testSubmitErrorDoesNotComplete(async: Async) {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		var completed = 0;
+		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"), () -> completed++);
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name == "iq" && stanza.attr.get("type") == "get") {
+				final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+				if (query != null) {
+					final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "result", id: stanza.attr.get("id"), from: "channel@example.com", to: "test@example.com" })
+						.tag("query", { xmlns: "http://jabber.org/protocol/muc#owner" })
+							.tag("x", { xmlns: "jabber:x:data", type: "form" })
+								.tag("field", { "var": "muc#roomconfig_roomname" })
+									.textTag("value", "Test Room")
+								.up()
+							.up()
+						.up();
+
+					client.stream.onStanza(responseStanza);
+					return EventHandled;
+				}
+			}
+			return EventUnhandled;
+		});
+
+		command.execute().then(session -> {
+			Assert.equals("executing", session.status);
+
+			client.stream.on("sendStanza", (stanza: Stanza) -> {
+				if (stanza.name == "iq" && stanza.attr.get("type") == "set") {
+					final query = stanza.getChild("query", "http://jabber.org/protocol/muc#owner");
+					if (query != null) {
+						final responseStanza = new Stanza("iq", { xmlns: "jabber:client", type: "error", id: stanza.attr.get("id"), from: "channel@example.com", to: "test@example.com" })
+							.tag("error", { type: "cancel" })
+								.tag("conflict", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" }).up()
+							.up();
+						client.stream.onStanza(responseStanza);
+						return EventHandled;
+					}
+				}
+				return EventUnhandled;
+			});
+
+			#if js
+			final data = {};
+			#else
+			final data = new borogove.Form.FormSubmitBuilder();
+			#end
+
+			session.execute(null, data, 0).then(nextSession -> {
+				Assert.equals("error", nextSession.status);
+				Assert.equals(1, nextSession.forms.length);
+				Assert.equals(0, completed);
 				async.done();
 			}, err -> {
 				Assert.fail(err);
@@ -78,7 +144,8 @@ class TestMucSettingsCommand extends utest.Test {
 	public function testExecuteGetError(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
-		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"));
+		var completed = 0;
+		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"), () -> completed++);
 
 		client.stream.on("sendStanza", (stanza: Stanza) -> {
 			if (stanza.name == "iq" && stanza.attr.get("type") == "get") {
@@ -99,6 +166,7 @@ class TestMucSettingsCommand extends utest.Test {
 		command.execute().then(session -> {
 			Assert.equals("error", session.status);
 			Assert.equals(1, session.forms.length);
+			Assert.equals(0, completed);
 			async.done();
 		}, err -> {
 			Assert.fail(err);
@@ -108,7 +176,8 @@ class TestMucSettingsCommand extends utest.Test {
 	public function testSessionCancel(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
-		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"));
+		var completed = 0;
+		final command = new MucSettingsCommand(client, JID.parse("channel@example.com"), () -> completed++);
 
 		final session = new borogove.MucSettingsCommand.MucSettingsCommandSession("executing", null, [], [], command);
 
@@ -119,6 +188,7 @@ class TestMucSettingsCommand extends utest.Test {
 
 		session.execute("cancel", null, 0).then(nextSession -> {
 			Assert.equals("canceled", nextSession.status);
+			Assert.equals(0, completed);
 			async.done();
 		}, err -> {
 			Assert.fail(err);