Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

14.4 KB
Raw Blame
import assert from 'node:assert/strict';
import test from 'node:test';
import { readFileSync } from 'node:fs';
import { loadBundled } from './bundle.mjs';

const { suggestedPrompts, roleOf, MAX_PROMPTS, MAX_TEAM_PROMPTS } = await loadBundled(`export * from './src/suggestedPrompts';`);

const DEV_PROMPTS = ['Analyze system architecture tradeoffs', 'Write a fast Python script for data processing', 'Review git status and security policies'];
const kitchen = [
  { name: 'HeadChef', title: 'Head Chef' },
  { name: 'Nutritionist', title: 'Nutritionist' },
  { name: 'Sous', title: 'Sous Chef' },
  { name: 'Pastry', title: 'Pastry Chef' },
  { name: 'Grocer', title: 'Grocery Planner' },
];

test("a kitchen team gets the lead's and two teammates' cooking asks, never developer prompts", () => {
  // What Oleks saw: a freshly hired Kitchen team offered "Analyze system architecture tradeoffs".
  const out = suggestedPrompts({ kind: 'team', members: kitchen, lead: 'HeadChef', purpose: 'Hired together: a team of 5.' });
  assert.deepEqual(out, [
    "@HeadChef plan this week's dinners",
    "@Nutritionist check this week's meals are balanced",
    '@Sous write a prep list for tonight',
    'What can each of you help me with?',
  ]);
  for (const p of DEV_PROMPTS) assert.ok(!out.includes(p));
});

test('the lead goes first even when listed last, and two bots with one role get one ask', () => {
  const out = suggestedPrompts({
    kind: 'team',
    members: [{ name: 'Cook2', title: 'Chef' }, { name: 'Cook1', title: 'Chef de cuisine' }, { name: 'Boss', title: 'Head Chef' }],
    lead: 'Boss',
  });
  assert.deepEqual(out, ["@Boss plan this week's dinners", 'What can each of you help me with?']);
});

test('an engineering team still gets engineering asks', () => {
  const out = suggestedPrompts({
    kind: 'team',
    members: [
      { name: 'Architect', title: 'Systems Architect' },
      { name: 'TechLead', title: 'Staff Engineer & Prototyper' },
      { name: 'DevOps', title: 'Systems & Security Engineer' },
    ],
    lead: 'Architect',
  });
  assert.deepEqual(out, [
    '@Architect sketch the architecture for a new feature',
    '@TechLead build a quick prototype',
    "@DevOps check my server's security",
    'What can each of you help me with?',
  ]);
});

test("unknown titles fall back to the team's purpose for the lead, else a neutral opener", () => {
  const members = [{ name: 'Ann', title: 'Wizard' }, { name: 'Bo', title: 'Sidekick' }];
  assert.deepEqual(suggestedPrompts({ kind: 'team', members, lead: 'Ann', purpose: 'Plans our weekly menu' }), [
    "@Ann plan this week's dinners",
    'What can each of you help me with?',
  ]);
  assert.deepEqual(suggestedPrompts({ kind: 'team', members, lead: 'Bo' }), ['@Bo what should we tackle first?', 'What can each of you help me with?']);
  assert.deepEqual(suggestedPrompts({ kind: 'team', members: [members[0]] }), ['@Ann what should we tackle first?', 'What can you help me with?']);
  assert.deepEqual(suggestedPrompts({ kind: 'team', members: [] }), []);
});

test('names that cannot be @mentioned are not used in a mention', () => {
  const out = suggestedPrompts({ kind: 'team', members: [{ name: 'Head Chef', title: 'Head Chef' }, { name: 'Sous', title: 'Sous Chef' }], lead: 'Head Chef' });
  assert.deepEqual(out, ['@Sous write a prep list for tonight', 'What can each of you help me with?']);
});

test("a one-to-one chat uses that bot's role, or its persona when the title says nothing", () => {
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: { name: 'Nutritionist', title: 'Nutritionist' } }), [
    "Check this week's meals are balanced",
    'Make a high-protein meal plan',
    'What can you help me with?',
  ]);
  assert.equal(suggestedPrompts({ kind: 'bot', bot: { name: 'X', title: 'Helper', persona_prompt: 'You are a seasoned SEO strategist.' } })[0], 'Find keywords I could rank for');
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: { name: 'X', title: 'Helper' } }), ['What can you help me with?']);
});

test("#general draws on the space's bots without naming them, and is empty with no bots", () => {
  const out = suggestedPrompts({ kind: 'general', bots: [{ name: 'DevOps', title: 'Systems & Security Engineer' }, ...kitchen] });
  assert.deepEqual(out, ["Check my server's security", "Plan this week's dinners", "Check this week's meals are balanced"]);
  assert.ok(out.every((p) => !p.includes('@')));
  assert.deepEqual(suggestedPrompts({ kind: 'general', bots: [] }), []);
});

test('#general with the Helpers team gives its three role asks and no filler (first-screen C)', () => {
  const helpers = [
    { name: 'Assistant', title: 'Personal Assistant' },
    { name: 'Researcher', title: 'Research Analyst' },
    { name: 'Writer', title: 'Writer & Editor' },
  ];
  const out = suggestedPrompts({ kind: 'general', bots: helpers });
  assert.deepEqual(out, ['Plan my week', 'Research my top 3 competitors', 'Outline a blog post']);
  assert.equal(out.length, MAX_PROMPTS);
  assert.ok(!out.includes('Who here can help me with what?'));
});

test('#general only adds "Who here can help me with what?" when fewer than three roles answer', () => {
  assert.deepEqual(suggestedPrompts({ kind: 'general', bots: [{ name: 'A', title: 'Personal Assistant' }, { name: 'W', title: 'Writer' }] }), [
    'Plan my week',
    'Outline a blog post',
    'Who here can help me with what?',
  ]);
  assert.deepEqual(suggestedPrompts({ kind: 'general', bots: [{ name: 'X', title: 'Wizard' }] }), ['Who here can help me with what?']);
  // The upgraded space's core-engineering starters: three distinct roles, so three asks.
  assert.deepEqual(
    suggestedPrompts({
      kind: 'general',
      bots: [
        { name: 'Architect', title: 'Systems Architect' },
        { name: 'TechLead', title: 'Staff Engineer & Prototyper' },
        { name: 'DevOps', title: 'Systems & Security Engineer' },
      ],
    }),
    ['Sketch the architecture for a new feature', 'Build a quick prototype', "Check my server's security"],
  );
});

test('never more than three (four in a team), each short enough for a phone chip', () => {
  const many = Array.from({ length: 30 }, (_, i) => ({ name: `B${i}`, title: ['Chef', 'Writer', 'Lawyer', 'Tutor', 'SEO lead'][i % 5] }));
  for (const ctx of [{ kind: 'team', members: many }, { kind: 'general', bots: many }, { kind: 'bot', bot: many[0] }]) {
    const out = suggestedPrompts(ctx);
    assert.ok(out.length <= (ctx.kind === 'team' ? MAX_TEAM_PROMPTS : MAX_PROMPTS), JSON.stringify(out));
    assert.ok(out.every((p) => p.length <= 50), JSON.stringify(out));
  }
});

test('specific roles win over general ones', () => {
  assert.equal(roleOf({ name: 'a', title: 'Sous Chef' }).asks[0], 'write a prep list for tonight');
  assert.equal(roleOf({ name: 'a', title: 'DevOps Engineer' }).asks[0], "check my server's security");
  assert.equal(roleOf({ name: 'a', title: 'QA and Release Engineer' }).asks[0], 'write a test plan for the next release');
  assert.equal(roleOf({ name: 'a', title: 'Frontend Engineer' }).asks[0], 'review the sign-up screen');
  assert.equal(roleOf({ name: 'a', title: 'Wizard' }), null);
});

test('the chat no longer hardcodes developer prompts', () => {
  const src = readFileSync(new URL('../src/components/ChatView.tsx', import.meta.url), 'utf8');
  for (const p of DEV_PROMPTS) assert.ok(!src.includes(p), p);
  const app = readFileSync(new URL('../src/App.tsx', import.meta.url), 'utf8');
  assert.match(app, /suggestedPrompts\(/);
});

// Rigel's review of #61: one qualifier word used to pick the field, so these got another trade's prompts.
const askOf = (title) => roleOf({ name: 'a', title })?.asks[0] ?? null;

test('a qualifier word does not pick the field: the job at the end of the title does', () => {
  assert.equal(askOf('Product Photographer'), 'describe a cover image idea');
  assert.equal(askOf('Interior Designer'), 'suggest a layout for my living room');
  assert.equal(askOf('Press Release Writer'), 'outline a blog post');
  assert.equal(askOf('Life Coach'), 'help me set goals for this month');
  // ...while the phrases that do name those fields still work.
  assert.equal(askOf('Product Manager'), 'turn this idea into a short spec');
  assert.equal(askOf('UI/UX Designer'), 'review the sign-up screen');
  assert.equal(askOf('Release Engineer'), 'write a test plan for the next release');
  assert.equal(askOf('Fitness Coach'), 'plan my workouts for the week');
  assert.equal(askOf('Platform Engineer'), "check my server's security");
});

test("a storytelling platform's purpose gets story prompts, not server security", () => {
  const out = suggestedPrompts({ kind: 'team', members: [{ name: 'Ann', title: 'Wizard' }, { name: 'Bo', title: 'Sidekick' }], lead: 'Ann', purpose: 'Platform for Kids Storytelling' });
  assert.deepEqual(out, ['@Ann write a bedtime story', 'What can each of you help me with?']);
  assert.equal(askOf('Platform for Kids Storytelling'), 'write a bedtime story');
});

test('the five titles from the review, as one team, each get their own field', () => {
  const out = suggestedPrompts({
    kind: 'team',
    members: [
      { name: 'Snap', title: 'Product Photographer' },
      { name: 'Deco', title: 'Interior Designer' },
      { name: 'Press', title: 'Press Release Writer' },
      { name: 'Guide', title: 'Life Coach' },
    ],
    lead: 'Snap',
  });
  assert.deepEqual(out, [
    '@Snap describe a cover image idea',
    '@Deco suggest a layout for my living room',
    '@Press outline a blog post',
    'What can each of you help me with?',
  ]);
  for (const p of out) assert.doesNotMatch(p, /spec|sign-up|test plan|workout|server/);
});

test('a member or bot with a missing name or title is skipped, not a crash', () => {
  const members = [{ name: undefined, title: 'Chef' }, { name: '  ', title: 'Chef' }, null, { name: 'Sous', title: undefined, persona_prompt: 42 }, { name: 'Nut', title: 'Nutritionist' }];
  assert.deepEqual(suggestedPrompts({ kind: 'team', members, lead: 'Nut' }), ["@Nut check this week's meals are balanced", 'What can each of you help me with?']);
  assert.deepEqual(suggestedPrompts({ kind: 'general', bots: [{ title: 'Chef' }, { name: 'Nut', title: 'Nutritionist' }] }), [
    "Check this week's meals are balanced",
    'Who here can help me with what?',
  ]);
  assert.deepEqual(suggestedPrompts({ kind: 'team', members: [{ title: 'Chef' }] }), []);
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: {} }), ['What can you help me with?']);
});

test('"engineer", "architect" and "test" only mean software when nothing else or a software word qualifies them', () => {
  const first = (title) => suggestedPrompts({ kind: 'bot', bot: { name: 'X', title } })[0];
  // Rigel's second review: these got prototype, architecture and test-plan prompts.
  assert.equal(first('Sound Engineer'), 'Pick background music for a story');
  assert.equal(first('Landscape Architect'), 'Plan a small vegetable garden');
  assert.equal(first('Interior Architect'), 'Suggest a layout for my living room');
  for (const title of ['Test Pilot', 'Civil Engineer', 'Naval Architect', 'Flight Test Engineer', 'Taste Tester', 'Property Developer']) {
    assert.equal(first(title), 'What can you help me with?', title);
  }
  // Software still reads as software, bare or qualified, in any case.
  assert.equal(first('Software Architect'), 'Sketch the architecture for a new feature');
  assert.equal(first('Cloud Architect'), 'Sketch the architecture for a new feature');
  for (const title of ['Engineer', 'Senior Engineer', 'Web Developer', 'Full-Stack Developer', 'software engineer', 'iOS Developer']) {
    assert.equal(first(title), 'Build a quick prototype', title);
  }
  for (const title of ['QA Engineer', 'Test Engineer', 'Software Tester', 'SDET']) {
    assert.equal(first(title), 'Write a test plan for the next release', title);
  }
  assert.equal(first('Penetration Tester'), "Check my server's security");
  // A persona is read the same way.
  assert.equal(suggestedPrompts({ kind: 'bot', bot: { name: 'X', persona_prompt: 'A civil engineer who reviews bridges.' } })[0], 'What can you help me with?');
  assert.equal(suggestedPrompts({ kind: 'bot', bot: { name: 'X', persona_prompt: 'You are an experienced software engineer.' } })[0], 'Build a quick prototype');
});

test('a storytelling team with a sound engineer gets no developer prompt, and an unmatched team gets the neutral opener', () => {
  const story = suggestedPrompts({
    kind: 'team',
    lead: 'Tale',
    purpose: 'Platform for Kids Storytelling',
    members: [
      { name: 'Tale', title: "Children's Book Author" },
      { name: 'Audio', title: 'Sound Engineer' },
    ],
  });
  assert.deepEqual(story, ['@Tale outline a blog post', '@Audio pick background music for a story', 'What can each of you help me with?']);
  const civil = suggestedPrompts({
    kind: 'team',
    lead: 'Bridge',
    members: [
      { name: 'Bridge', title: 'Civil Engineer' },
      { name: 'Pilot', title: 'Test Pilot' },
    ],
  });
  assert.deepEqual(civil, ['@Bridge what should we tackle first?', 'What can each of you help me with?']);
});

// ha-sheet-polish (c): House, the Home template's lead, gets home chips, not "Plan my week".
const house = {
  name: 'House', title: 'Home Helper',
  persona_prompt: 'You are House, the lead of the @home team. You look after one home through Home Assistant. You answer questions about the home.',
};

test('House in a 1:1 gets home asks, and the persona order does not matter', () => {
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: house }), ['Is anything left on?', 'Turn off all the lights', 'What can you help me with?']);
  // The title alone is enough; a persona that only says "Home Assistant" is too
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: { name: 'H', title: 'Home Helper' } })[0], 'Is anything left on?');
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: { name: 'H', title: '', persona_prompt: 'You run the smart home through Home Assistant.' } })[0], 'Is anything left on?');
  assert.deepEqual(suggestedPrompts({ kind: 'bot', bot: { name: 'H', title: 'Home Assistant' } })[0], 'Is anything left on?');
});

test('an Assistant or a Planner bot still gets "Plan my week"', () => {
  for (const title of ['Assistant', 'Personal Assistant', 'Planner', 'Executive Assistant']) {
    assert.equal(suggestedPrompts({ kind: 'bot', bot: { name: 'A', title } })[0], 'Plan my week', title);
  }
});

test("@home (a team chat) shows House's asks", () => {
  assert.deepEqual(suggestedPrompts({ kind: 'team', members: [house], lead: 'House' }), ['@House is anything left on?', 'What can you help me with?']);
});