191 lines
4.5 KiB
JavaScript
191 lines
4.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
normalizeDomainInput,
|
|
resolveIncomingIframeSelection,
|
|
resolveConfiguredIframeAgentId,
|
|
resolveDomainMappedAgentId,
|
|
} from "./domain-agent.ts";
|
|
|
|
test("normalizes full urls and bare hosts to comparable domains", () => {
|
|
assert.equal(
|
|
normalizeDomainInput(" https://Foo.Example.com/path?q=1 "),
|
|
"foo.example.com",
|
|
);
|
|
assert.equal(
|
|
normalizeDomainInput("foo.example.com:8080/landing"),
|
|
"foo.example.com:8080",
|
|
);
|
|
});
|
|
|
|
test("resolves the configured agent for a matching pageContext url", () => {
|
|
assert.equal(
|
|
resolveDomainMappedAgentId({
|
|
pageUrl: "https://foo.example.com/article/123",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
}),
|
|
"agent-1",
|
|
);
|
|
});
|
|
|
|
test("ignores disabled mappings and unavailable agents", () => {
|
|
assert.equal(
|
|
resolveDomainMappedAgentId({
|
|
pageUrl: "https://foo.example.com/article/123",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: false,
|
|
},
|
|
{
|
|
id: "m2",
|
|
domain: "bar.example.com",
|
|
agentId: "agent-2",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-3"],
|
|
}),
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("does not auto-select after manual selection or once a conversation has started", () => {
|
|
assert.equal(
|
|
resolveConfiguredIframeAgentId({
|
|
pageUrl: "https://foo.example.com",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1"],
|
|
isNewThread: true,
|
|
hasStartedConversation: false,
|
|
manualSelectionLocked: true,
|
|
}),
|
|
undefined,
|
|
);
|
|
|
|
assert.equal(
|
|
resolveConfiguredIframeAgentId({
|
|
pageUrl: "https://foo.example.com",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1"],
|
|
isNewThread: true,
|
|
hasStartedConversation: true,
|
|
manualSelectionLocked: false,
|
|
}),
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("falls back to default assistant when url is unmapped and user has not manually selected an agent", () => {
|
|
assert.deepEqual(
|
|
resolveIncomingIframeSelection({
|
|
pageUrl: "https://unknown.example.com",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
manualSelectedAgentId: null,
|
|
}),
|
|
{
|
|
selectedAgentId: null,
|
|
forceDefaultAssistant: true,
|
|
},
|
|
);
|
|
});
|
|
|
|
test("falls back to the user's manual selection when url is unmapped", () => {
|
|
assert.deepEqual(
|
|
resolveIncomingIframeSelection({
|
|
pageUrl: "https://unknown.example.com",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
manualSelectedAgentId: "agent-2",
|
|
}),
|
|
{
|
|
selectedAgentId: "agent-2",
|
|
forceDefaultAssistant: false,
|
|
},
|
|
);
|
|
});
|
|
|
|
test("mapped domain overrides the user's manual selection for that incoming message", () => {
|
|
assert.deepEqual(
|
|
resolveIncomingIframeSelection({
|
|
pageUrl: "https://foo.example.com/article",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
manualSelectedAgentId: "agent-2",
|
|
}),
|
|
{
|
|
selectedAgentId: "agent-1",
|
|
forceDefaultAssistant: false,
|
|
},
|
|
);
|
|
});
|
|
|
|
test("unmapped incoming url resets auto-selected agent back to default when there is no manual selection", () => {
|
|
assert.deepEqual(
|
|
resolveIncomingIframeSelection({
|
|
pageUrl: "https://another-unknown.example.com/article",
|
|
mappings: [
|
|
{
|
|
id: "m1",
|
|
domain: "foo.example.com",
|
|
agentId: "agent-1",
|
|
enabled: true,
|
|
},
|
|
],
|
|
availableAgentIds: ["agent-1", "agent-2"],
|
|
manualSelectedAgentId: null,
|
|
}),
|
|
{
|
|
selectedAgentId: null,
|
|
forceDefaultAssistant: true,
|
|
},
|
|
);
|
|
});
|