Files
tribes-plugin-aether/test/aether/external_client_interop_test.exs
self c1f4339dde refactor: move Aether into TribeOne plugin namespace
Use TribeOne.TribesPlugin.Aether modules throughout the plugin and expose chat@1 from the entry module for capability-based consumers.
2026-05-26 01:13:28 +02:00

91 lines
3.1 KiB
Elixir

defmodule TribeOne.TribesPlugin.Aether.ExternalClientInteropTest do
use ExUnit.Case, async: false
test "nak can produce and consume NIP-17/NIP-59 and NIP-04 DM artifacts" do
require_executable!("nak")
output =
run_script!(~S'''
set -euo pipefail
sender_sec=$(nak key generate)
recipient_sec=$(nak key generate)
sender_pub=$(nak key public "$sender_sec")
recipient_pub=$(nak key public "$recipient_sec")
gift=$(nak event --sec "$sender_sec" -k 14 -p "$recipient_pub" -c "hello from nak" </dev/null 2>/dev/null | nak gift wrap --sec "$sender_sec" -p "$recipient_pub" 2>/dev/null)
rumor=$(printf '%s\n' "$gift" | nak gift unwrap --sec "$recipient_sec" 2>/dev/null)
legacy_ciphertext=$(nak encrypt --nip04 --sec "$sender_sec" -p "$recipient_pub" "legacy hello from nak" </dev/null 2>/dev/null)
legacy_plaintext=$(nak decrypt --nip04 --sec "$recipient_sec" -p "$sender_pub" "$legacy_ciphertext" 2>/dev/null)
printf 'SENDER_PUB\t%s\n' "$sender_pub"
printf 'RECIPIENT_PUB\t%s\n' "$recipient_pub"
printf 'GIFT\t%s\n' "$gift"
printf 'RUMOR\t%s\n' "$rumor"
printf 'NIP04\t%s\n' "$legacy_plaintext"
''')
lines = prefixed_lines(output)
sender_pub = Map.fetch!(lines, "SENDER_PUB")
recipient_pub = Map.fetch!(lines, "RECIPIENT_PUB")
gift = lines |> Map.fetch!("GIFT") |> JSON.decode!()
rumor = lines |> Map.fetch!("RUMOR") |> JSON.decode!()
assert gift["kind"] == 1059
assert ["p", recipient_pub] in Enum.map(gift["tags"], &Enum.take(&1, 2))
assert rumor["kind"] == 14
assert rumor["pubkey"] == sender_pub
assert rumor["content"] == "hello from nak"
assert ["p", recipient_pub] in Enum.map(rumor["tags"], &Enum.take(&1, 2))
assert Map.fetch!(lines, "NIP04") == "legacy hello from nak"
end
test "algia can consume Nostr event JSON with isolated config" do
require_executable!("nak")
require_executable!("algia")
output =
run_script!(~S'''
set -euo pipefail
home=$(mktemp -d)
mkdir -p "$home/.config/algia"
sec=$(nak key generate)
nsec=$(nak encode nsec "$sec")
printf '{"privateKey":"%s","relays":{}}\n' "$nsec" > "$home/.config/algia/config.json"
nak event --sec "$sec" -k 1 -c "hello from algia cat" </dev/null 2>/dev/null | HOME="$home" algia cat --json 2>/dev/null
''')
event = JSON.decode!(String.trim(output))
assert event["kind"] == 1
assert event["content"] == "hello from algia cat"
assert is_binary(event["id"])
assert is_binary(event["sig"])
end
defp require_executable!(name) do
unless System.find_executable(name) do
flunk("expected #{name} in PATH")
end
end
defp run_script!(script) do
case System.cmd("timeout", ["30s", "bash", "-lc", script], stderr_to_stdout: true) do
{output, 0} -> output
{output, status} -> flunk("script failed with status #{status}:\n#{output}")
end
end
defp prefixed_lines(output) do
output
|> String.split("\n", trim: true)
|> Map.new(fn line ->
[key, value] = String.split(line, "\t", parts: 2)
{key, value}
end)
end
end