~singpolyma/dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 3

[PATCH 1/2] add new admin command to launch snikket instance for existing customer

Details
Message ID
<20240327203630.22572-1-sourcehut@lazytapir.com>
DKIM signature
pass
Download raw message
Patch: +43 -9
---
 forms/admin_menu.rb                 |  3 ++-
 lib/admin_actions/launch_snikket.rb | 26 ++++++++++++++++++++++++++
 lib/admin_command.rb                | 23 +++++++++++++++--------
 3 files changed, 43 insertions(+), 9 deletions(-)
 create mode 100644 lib/admin_actions/launch_snikket.rb

diff --git a/forms/admin_menu.rb b/forms/admin_menu.rb
index 2d46719..01e1738 100644
--- a/forms/admin_menu.rb
+++ b/forms/admin_menu.rb
@@ -24,6 +24,7 @@ field(
		{ value: "set_trust_level", label: "Set Trust Level" },
		{ value: "add_invites", label: "Add Referral Codes" },
		{ value: "number_change", label: "Number Change" },
		{ value: "add_transaction", label: "Add Transaction" }
		{ value: "add_transaction", label: "Add Transaction" },
		{ value: "launch_snikket", label: "Launch Snikket" }
	]
)
diff --git a/lib/admin_actions/launch_snikket.rb b/lib/admin_actions/launch_snikket.rb
new file mode 100644
index 0000000..98f9661
--- /dev/null
+++ b/lib/admin_actions/launch_snikket.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class AdminAction
	class LaunchSnikket
		def self.call(customer_id, reply:, snikket_repo:, **)
			reply.call(
				FormTemplate.render("snikket_launch")
			).then { |response|
				domain = response.form.field("domain").value.to_s
				IQ_MANAGER.write(Snikket::Launch.new(
					nil, CONFIG[:snikket_hosting_api],
					domain: domain
				)).then do |launched|
					Snikket::CustomerInstance.for(customer_id, domain, launched)
				end
			}.then { |instance|
				snikket_repo.put(instance).then do
					reply.call(FormTemplate.render(
						"snikket_launched",
						instance: instance
					))
				end
			}
		end
	end
end
diff --git a/lib/admin_command.rb b/lib/admin_command.rb
index c35d48a..101030f 100644
--- a/lib/admin_command.rb
+++ b/lib/admin_command.rb
@@ -8,6 +8,7 @@ require_relative "admin_actions/financial"
require_relative "admin_actions/reset_declines"
require_relative "admin_actions/set_trust_level"
require_relative "admin_actions/number_change"
require_relative "admin_actions/launch_snikket"
require_relative "bill_plan_command"
require_relative "customer_info_form"
require_relative "financial_info"
@@ -17,10 +18,11 @@ class AdminCommand
	def self.for(
		target_customer,
		customer_repo,
		admin_action_repo=AdminActionRepo.new
		admin_action_repo=AdminActionRepo.new,
		snikket_repo=Snikket::Repo.new
	)
		if target_customer
			new(target_customer, customer_repo, admin_action_repo)
			new(target_customer, customer_repo, admin_action_repo, snikket_repo)
		else
			NoUser.new(customer_repo, admin_action_repo, notice: "Customer Not Found")
		end
@@ -51,11 +53,13 @@ class AdminCommand
	def initialize(
		target_customer,
		customer_repo,
		admin_action_repo=AdminActionRepo.new
		admin_action_repo=AdminActionRepo.new,
		snikket_repo=Snikket::Repo.new
	)
		@target_customer = target_customer
		@customer_repo = customer_repo
		@admin_action_repo = admin_action_repo
		@snikket_repo = snikket_repo
	end

	def start(command_action=:execute)
@@ -98,7 +102,7 @@ class AdminCommand
	def new_context(q, command_action=:execute)
		CustomerInfoForm.new(@customer_repo)
			.parse_something(q).then do |new_customer|
				AdminCommand.for(new_customer, @customer_repo, @admin_action_repo)
				AdminCommand.for(new_customer, @customer_repo, @admin_action_repo, @snikket_repo)
					.then { |ac| ac.start(command_action) }
			end
	end
@@ -142,11 +146,12 @@ class AdminCommand
			@klass = klass
		end

		def call(customer_id, customer_repo:, **)
		def call(customer_id, customer_repo:, snikket_repo:, **)
			@klass.call(
				customer_id,
				reply: method(:reply),
				customer_repo: customer_repo
				customer_repo: customer_repo,
				snikket_repo: snikket_repo
			).then { nil }
		end

@@ -180,13 +185,15 @@ class AdminCommand
		[:set_trust_level, Undoable.new(AdminAction::SetTrustLevel::Command)],
		[:add_invites, Undoable.new(AdminAction::AddInvites::Command)],
		[:number_change, Undoable.new(AdminAction::NumberChange::Command)],
		[:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)]
		[:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)],
		[:launch_snikket, Simple.new(AdminAction::LaunchSnikket)]
	].each do |action, handler|
		define_method("action_#{action}") do
			handler.call(
				@target_customer,
				admin_action_repo: @admin_action_repo,
				customer_repo: @customer_repo
				customer_repo: @customer_repo,
				snikket_repo: @snikket_repo
			)
		end
	end
-- 
2.44.0

[PATCH 2/2] test for admin launch snikket command

Details
Message ID
<20240327203630.22572-2-sourcehut@lazytapir.com>
In-Reply-To
<20240327203630.22572-1-sourcehut@lazytapir.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +63 -1
---
 test/test_admin_command.rb | 64 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/test/test_admin_command.rb b/test/test_admin_command.rb
index 16730db..7bb9211 100644
--- a/test/test_admin_command.rb
+++ b/test/test_admin_command.rb
@@ -4,17 +4,79 @@ require "test_helper"
require "admin_command"

BackendSgx::IQ_MANAGER = Minitest::Mock.new
AdminAction::LaunchSnikket::IQ_MANAGER = Minitest::Mock.new
Customer::BLATHER = Minitest::Mock.new
AdminActionRepo::REDIS = Minitest::Mock.new
DB = FakeDB.new

class AdminCommandTest < Minitest::Test
	def admin_command(tel="+15556667777")
		sgx = Minitest::Mock.new(OpenStruct.new(
			registered?: OpenStruct.new(phone: tel)
		))
		[sgx, AdminCommand.new(customer(sgx: sgx), CustomerRepo.new)]
		[sgx, AdminCommand.new(customer(sgx: sgx), CustomerRepo.new, Snikket::Repo.new)]
	end

	def test_action_launch_snikket
		sgx, admin = admin_command
		domain_form = Blather::Stanza::Iq::Command.new
		domain_form.form.fields = [
			{ var: "domain", value: "test.snikket.chat" }
		]

		launched = Snikket::Launched.new
		launched << Niceogiri::XML::Node.new(
			:launched, launched.document, "xmpp:snikket.org/hosting/v1"
		).tap { |inner|
			inner << Niceogiri::XML::Node.new(
				:'instance-id', launched.document, "xmpp:snikket.org/hosting/v1"
			).tap { |id|
				id.content = "si-1234"
			}
			inner << Niceogiri::XML::Node.new(
				:bootstrap, launched.document, "xmpp:snikket.org/hosting/v1"
			).tap { |bootstrap|
				bootstrap << Niceogiri::XML::Node.new(
					:token, launched.document, "xmpp:snikket.org/hosting/v1"
				).tap { |token|
					token.content = "TOKEN"
				}
			}
		}

		Command::COMMAND_MANAGER.expect(
			:write,
			EMPromise.resolve(domain_form),
			[Matching.new do |iq|
				assert_equal :form, iq.form.type
				assert iq.form.field("domain")
			end]
		)

		AdminAction::LaunchSnikket::IQ_MANAGER.expect(
			:write,
			EMPromise.resolve(launched),
			[Matching.new do |iq|
				assert_equal :set, iq.type
				assert_equal CONFIG[:snikket_hosting_api], iq.to.to_s
				assert_equal(
					"test.snikket.chat",
					iq.xpath(
						"./ns:launch/ns:domain",
						ns: "xmpp:snikket.org/hosting/v1"
					).text
				)
			end]
		)

		execute_command { admin.action_launch_snikket }

		assert_mock sgx
		assert_mock AdminAction::LaunchSnikket::IQ_MANAGER
		assert_mock Customer::BLATHER
	end
	em :test_action_launch_snikket

	def test_action_cancel_account
		sgx, admin = admin_command

-- 
2.44.0

Re: [PATCH 2/2] test for admin launch snikket command

BloomingFlower <sourcehut@lazytapir.com>
Details
Message ID
<eac9bedd-eb36-4292-84bb-a8db858097ae@lazytapir.com>
In-Reply-To
<20240327203630.22572-2-sourcehut@lazytapir.com> (view parent)
DKIM signature
pass
Download raw message
Not sure what to do about this.

1) Error:

AdminCommandTest#test_action_launch_snikket:
NoMethodError: undefined method `reply' for nil:NilClass
     /home/peanut/jmp/sgx-jmp/lib/command.rb:19:in `reply'
     /home/peanut/jmp/sgx-jmp/lib/admin_command.rb:159:in `reply'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `call'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `block (2 levels) in call'

Re: [PATCH 2/2] test for admin launch snikket command

Details
Message ID
<ZgSGwyj92mdHHSI9@singpolyma-beefy.lan>
In-Reply-To
<eac9bedd-eb36-4292-84bb-a8db858097ae@lazytapir.com> (view parent)
DKIM signature
pass
Download raw message
>NoMethodError: undefined method `reply' for nil:NilClass
>    /home/peanut/jmp/sgx-jmp/lib/command.rb:19:in `reply'

This is because there is no command execution in the current Fiber context.  
While you did wrap the invocation in execute_command {} you have higher up 
in your test code creating promises that will be injected which are being 
created outside of the execution context (and this bind to a different 
Fiber).

So move the exectue_command { start of the block up to before your 
IQ_MANAGER and COMMAND_MANAGER expects in order to fix it.
Reply to thread Export thread (mbox)