The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
test(assistant): load the tool cap's groups without awaiting in a loop
Maurice98c883b4e836
diff --git a/server/tests/unit/nest/assistant/assistant-tool-cap.test.ts b/server/tests/unit/nest/assistant/assistant-tool-cap.test.tsindex 592b8b730..88a5312e8 100644--- a/server/tests/unit/nest/assistant/assistant-tool-cap.test.ts+++ b/server/tests/unit/nest/assistant/assistant-tool-cap.test.ts@@ -31,7 +31,7 @@ beforeAll(async () => {orm = app.get(MikroORM);tools = app.get(AssistantToolsService);// Every addon on, so every group holds all it can.- for (const addon of Object.values(ADDON_IDS)) await setAddonEnabled(orm, addon, true);+ await Promise.all(Object.values(ADDON_IDS).map((addon) => setAddonEnabled(orm, addon, true)));await setAppSetting(orm, 'tours_wanderer_enabled', 'true');});@@ -41,110 +41,131 @@ async function tripSession(): Promise<AssistantToolSession> {return tools.open(user.id, trip.id);}+/** Runs `use` on the session and closes it whatever happens. */+async function within<T>(+ opening: Promise<AssistantToolSession>,+ use: (session: AssistantToolSession) => Promise<T>,+): Promise<T> {+ const session = await opening;+ try {+ return await use(session);+ } finally {+ await session.close();+ }+}+const names = (session: AssistantToolSession) => session.specs().map((spec) => spec.name);+interface LoadAnswer {+ loaded: string[];+ tools: string[];+ unloaded?: string[];+ notLoaded?: string[];+ note?: string;+}++interface Step {+ group: string;+ answer: LoadAnswer;+ offered: string[];+}++/** Loads the groups one step after another, as a model would over a turn, with what each step offered. */+async function loadOneByOne(session: AssistantToolSession, groups: readonly string[]): Promise<Step[]> {+ const [group, ...rest] = groups;+ if (group === undefined) return [];+ const run = await session.call('use_addon_tools', { groups: [group] });+ const step = { group, answer: run.value as LoadAnswer, offered: names(session) };+ return [step, ...(await loadOneByOne(session, rest))];+}+describe('the tools one step offers', () => {it('ASTCAP-001: a trip chat starts within the limit, with room for any one group beside the core', async () => {- const session = await tripSession();- try {- expect(session.specs().length).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- for (const group of GROUPS) {- const one = await tripSession();- try {- const run = await one.call('use_addon_tools', { groups: [group] });- expect(run.ok, group).toBe(true);- expect((run.value as { loaded: string[] }).loaded, group).toEqual([group]);- expect(one.specs().length, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- } finally {- await one.close();- }- }- } finally {- await session.close();+ const start = await within(tripSession(), (session) => Promise.resolve(session.specs().length));+ expect(start).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);+ const counts = await Promise.all(+ GROUPS.map((group) =>+ within(tripSession(), async (session) => {+ const run = await session.call('use_addon_tools', { groups: [group] });+ return { group, ok: run.ok, loaded: (run.value as LoadAnswer).loaded, offered: session.specs().length };+ }),+ ),+ );+ for (const { group, ok, loaded, offered } of counts) {+ expect(ok, group).toBe(true);+ expect(loaded, group).toEqual([group]);+ expect(offered, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);}});it('ASTCAP-002: loading one group after another unloads the one used longest ago, never the one just asked for', async () => {- const session = await tripSession();- try {- const unloadedSeen: string[] = [];- for (const group of GROUPS) {- const run = await session.call('use_addon_tools', { groups: [group] });- const value = run.value as { loaded: string[]; tools: string[]; unloaded?: string[] };- expect(value.loaded).toEqual([group]);- expect(value.unloaded ?? [], group).not.toContain(group);- unloadedSeen.push(...(value.unloaded ?? []));- const offered = names(session);- expect(offered.length, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- for (const tool of value.tools) expect(offered, `${group}: ${tool}`).toContain(tool);- }- // Every group cannot fit at once, so the walk had to make room, oldest first.- expect(unloadedSeen.length).toBeGreaterThan(0);- expect(unloadedSeen[0]).toBe(GROUPS[0]);- } finally {- await session.close();+ const steps = await within(tripSession(), (session) => loadOneByOne(session, GROUPS));+ for (const { group, answer, offered } of steps) {+ expect(answer.loaded).toEqual([group]);+ expect(answer.unloaded ?? [], group).not.toContain(group);+ expect(offered.length, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);+ for (const tool of answer.tools) expect(offered, `${group}: ${tool}`).toContain(tool);}+ // Every group cannot fit at once, so the walk had to make room, oldest first.+ const unloaded = steps.flatMap((step) => step.answer.unloaded ?? []);+ expect(unloaded.length).toBeGreaterThan(0);+ expect(unloaded[0]).toBe(GROUPS[0]);});it('ASTCAP-003: asking for every group at once loads what fits and names the rest', async () => {- const session = await tripSession();- try {+ await within(tripSession(), async (session) => {const run = await session.call('use_addon_tools', { groups: GROUPS });- const value = run.value as { loaded: string[]; notLoaded?: string[]; note?: string };+ const value = run.value as LoadAnswer;expect(session.specs().length).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);expect(value.loaded.length).toBeGreaterThan(0);expect([...value.loaded, ...(value.notLoaded ?? [])].sort()).toEqual([...GROUPS].sort());expect(value.notLoaded?.length ?? 0).toBeGreaterThan(0);expect(value.note).toMatch(/on their own/);- } finally {- await session.close();- }+ });});it('ASTCAP-004: a tool of a group that was unloaded loads it again when the model calls it', async () => {- const session = await tripSession();- try {- const first = GROUPS[0]!;- const run = await session.call('use_addon_tools', { groups: [first] });- const tool = (run.value as { tools: string[] }).tools[0]!;- // Load the others until the first one is gone again.- for (const group of GROUPS.slice(1)) {- await session.call('use_addon_tools', { groups: [group] });- if (!names(session).includes(tool)) break;- }+ await within(tripSession(), async (session) => {+ const [first, ...others] = GROUPS;+ const [loadedFirst] = await loadOneByOne(session, first === undefined ? [] : [first]);+ const tool = String(loadedFirst?.answer.tools[0]);+ expect(loadedFirst?.offered).toContain(tool);+ // Loading every other group makes room by unloading the first one, used longest ago.+ await loadOneByOne(session, others);expect(names(session)).not.toContain(tool);expect(session.pinArgs(tool, {})).not.toHaveProperty('error');expect(names(session)).toContain(tool);expect(session.specs().length).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- } finally {- await session.close();- }+ });});it('ASTCAP-005: the home chat stays within the limit with any one group loaded', async () => {const { user } = await makeUser(orm);- for (const group of GROUPS) {- const session = await tools.openHome(user.id);- try {- expect(session.specs().length, 'home').toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- await session.call('use_addon_tools', { groups: [group] });- expect(session.specs().length, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- } finally {- await session.close();- }+ const counts = await Promise.all(+ GROUPS.map((group) =>+ within(tools.openHome(user.id), async (session) => {+ const before = session.specs().length;+ await session.call('use_addon_tools', { groups: [group] });+ return { group, before, after: session.specs().length };+ }),+ ),+ );+ for (const { group, before, after } of counts) {+ expect(before, 'home').toBeLessThanOrEqual(MAX_OFFERED_TOOLS);+ expect(after, group).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);}});it('ASTCAP-006: a journey chat offers no more than the limit on either of its pages', async () => {const { user } = await makeUser(orm);const journey = await makeJourney(orm, user.id, { title: 'Lisbon' });- for (const surface of ['studio', 'journal'] as const) {- const session = await tools.openJourney(user.id, journey.id, { role: 'owner', surface });- try {- expect(session.specs().length, surface).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);- } finally {- await session.close();- }- }+ const counts = await Promise.all(+ (['studio', 'journal'] as const).map((surface) =>+ within(tools.openJourney(user.id, journey.id, { role: 'owner', surface }), (session) =>+ Promise.resolve({ surface, offered: session.specs().length }),+ ),+ ),+ );+ for (const { surface, offered } of counts) expect(offered, surface).toBeLessThanOrEqual(MAX_OFFERED_TOOLS);});});