Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions desktop/src/features/agents/lib/availableRelayAgents.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from "node:assert/strict";
import test from "node:test";

import { availableRelayAgents } from "./availableRelayAgents.ts";

const ME = "a".repeat(64);
const OTHER = "b".repeat(64);

function agent(name, pubkey, overrides = {}) {
return {
pubkey,
ownerPubkey: null,
name,
agentType: "relay",
channels: [],
channelIds: [],
capabilities: [],
status: "offline",
respondTo: "owner-only",
respondToAllowlist: [],
...overrides,
};
}

test("lists only relay agents the current identity may instruct", () => {
const result = availableRelayAgents(
[
agent("Pokebalzer", "1".repeat(64), {
respondTo: "allowlist",
respondToAllowlist: [ME],
status: "online",
}),
agent("Relay Master", "2".repeat(64), { ownerPubkey: ME }),
agent("Someone else's", "3".repeat(64), { ownerPubkey: OTHER }),
agent("Shared channel", "4".repeat(64), {
respondTo: "anyone",
channelIds: ["general"],
}),
agent("Unshared", "5".repeat(64), {
respondTo: "anyone",
channelIds: ["private-elsewhere"],
}),
],
new Set(["general"]),
ME,
);

assert.deepEqual(
result.map(({ name }) => name),
["Pokebalzer", "Relay Master", "Shared channel"],
);
});

test("deduplicates pubkeys and keeps status-first ordering", () => {
const duplicate = "6".repeat(64);
const result = availableRelayAgents(
[
agent("Offline", "7".repeat(64), {
respondTo: "allowlist",
respondToAllowlist: [ME],
}),
agent("Online", duplicate.toUpperCase(), {
respondTo: "allowlist",
respondToAllowlist: [ME],
status: "online",
}),
agent("Duplicate", duplicate, {
respondTo: "allowlist",
respondToAllowlist: [ME],
}),
],
new Set(),
ME,
);

assert.deepEqual(
result.map(({ name }) => name),
["Online", "Offline"],
);
});
34 changes: 34 additions & 0 deletions desktop/src/features/agents/lib/availableRelayAgents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { relayAgentIsSharedWithUser } from "./agentAutocompleteEligibility";
import type { RelayAgent } from "@/shared/api/types";
import { normalizePubkey } from "@/shared/lib/pubkey";

const STATUS_PRIORITY: Record<RelayAgent["status"], number> = {
online: 0,
away: 1,
offline: 2,
};

/** Relay agents the current identity can actually instruct. */
export function availableRelayAgents(
relayAgents: readonly RelayAgent[] | undefined,
sharedChannelIds: ReadonlySet<string>,
currentPubkey?: string | null,
): RelayAgent[] {
const seen = new Set<string>();

return (relayAgents ?? [])
.filter((agent) => {
const pubkey = normalizePubkey(agent.pubkey);
if (seen.has(pubkey)) return false;
if (!relayAgentIsSharedWithUser(agent, sharedChannelIds, currentPubkey)) {
return false;
}
seen.add(pubkey);
return true;
})
.sort((left, right) => {
const status =
STATUS_PRIORITY[left.status] - STATUS_PRIORITY[right.status];
return status || left.name.localeCompare(right.name);
});
}
Loading