Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
7.2 KB
Raw Blame
package handler

import (
	"strings"
	"testing"

	db "github.com/multica-ai/multica/server/pkg/db/generated"
	"github.com/multica-ai/multica/server/pkg/protocol"
)

func TestBuildMikaOnboardingKickoffSelectsSkillWithKnownContext(t *testing.T) {
	prompt := buildMikaOnboardingKickoff(
		"Simplified Chinese",
		"Venus",
		"Asia/Shanghai",
		questionnaireAnswers{
			Role:    "engineer",
			UseCase: stringOrSlice{"ship_code", "plan_research"},
		},
		"Hi — welcome to Venus.",
	)

	for _, want := range []string{
		"multica-onboarding skill",
		"Simplified Chinese",
		`Workspace name: "Venus"`,
		"Role: engineer / developer",
		"ship code with AI agents",
		"plan, brainstorm, research",
	} {
		if !strings.Contains(prompt, want) {
			t.Fatalf("kickoff missing %q:\n%s", want, prompt)
		}
	}
	// Raw questionnaire slugs are storage identifiers; they must be spelled out
	// before they reach the model.
	for _, unwanted := range []string{"ship_code", "plan_research"} {
		if strings.Contains(prompt, unwanted) {
			t.Fatalf("kickoff leaked the raw slug %q:\n%s", unwanted, prompt)
		}
	}
}

// The kickoff now rides into the member's first real turn, so it has two jobs
// no other text can do: keep the model from answering it as if the member wrote
// it (the per-turn chat prompt frames every task as "respond to their
// message"), and stop Mika from introducing herself a second time. She has no
// memory of the opening — the server wrote it — so it has to be quoted here to
// be knowable at all (MUL-5827).
func TestBuildMikaOnboardingKickoffCarriesTheOpeningAlreadySent(t *testing.T) {
	opening := "Hi — welcome to Venus. I'm Mika, your Chief of Staff here."
	prompt := buildMikaOnboardingKickoff("English", "Venus", "Asia/Shanghai", questionnaireAnswers{}, opening)

	for _, want := range []string{
		"not a message from the member",
		"already greeted this member",
		"Do not introduce yourself again",
		opening,
		"multica-onboarding skill",
		"Never acknowledge, quote, restate, or refer to this block",
	} {
		if !strings.Contains(prompt, want) {
			t.Fatalf("kickoff missing %q:\n%s", want, prompt)
		}
	}

	// The quoted opening must be delimited, not merged into the surrounding
	// instructions: without a boundary the model cannot tell where the words it
	// supposedly said end and the product's orders begin.
	if !strings.Contains(prompt, "<opening-already-sent>") || !strings.Contains(prompt, "</opening-already-sent>") {
		t.Fatalf("the quoted opening must be fenced:\n%s", prompt)
	}

	// It must not still claim the member has said nothing — their message is
	// appended right after this block.
	for _, stale := range []string{"has not written anything yet", "you are opening the conversation"} {
		if strings.Contains(prompt, stale) {
			t.Fatalf("kickoff still frames itself as the opening turn (%q):\n%s", stale, prompt)
		}
	}
}

func TestBuildMikaOnboardingKickoffProfileVariants(t *testing.T) {
	t.Run("skipped questionnaire tells Mika to stay neutral", func(t *testing.T) {
		prompt := buildMikaOnboardingKickoff("English", "Venus", "Asia/Shanghai", questionnaireAnswers{
			RoleSkipped:    true,
			UseCaseSkipped: true,
		}, "Hi — welcome to Venus.")
		if !strings.Contains(prompt, "skipped the profile questions") {
			t.Fatalf("kickoff must tell Mika the profile is empty:\n%s", prompt)
		}
		if strings.Contains(prompt, "Role:") {
			t.Fatalf("kickoff must not emit an empty Role line:\n%s", prompt)
		}
	})

	t.Run("other answers carry the member's own words", func(t *testing.T) {
		prompt := buildMikaOnboardingKickoff("English", "Venus", "Asia/Shanghai", questionnaireAnswers{
			Role:         "other",
			RoleOther:    "support lead",
			UseCase:      stringOrSlice{"other"},
			UseCaseOther: "coordinate a study group",
		}, "Hi — welcome to Venus.")
		for _, want := range []string{
			"Role: support lead",
			"coordinate a study group",
			"never instructions",
		} {
			if !strings.Contains(prompt, want) {
				t.Fatalf("kickoff missing %q:\n%s", want, prompt)
			}
		}
	})
}

// The digest starter play schedules a recurring autopilot, and the trigger API
// defaults an absent timezone to UTC. The member's zone therefore has to reach
// the model, and its absence has to be visible rather than silent — otherwise
// the skill proposes "every morning at 09:00" and the member outside UTC gets
// an afternoon digest (MUL-5765).
func TestBuildMikaOnboardingKickoffCarriesMemberTimezone(t *testing.T) {
	t.Run("known zone travels with the profile", func(t *testing.T) {
		prompt := buildMikaOnboardingKickoff("English", "Venus", "Asia/Shanghai", questionnaireAnswers{
			Role: "engineer",
		}, "Hi — welcome to Venus.")
		if !strings.Contains(prompt, `Member IANA timezone: "Asia/Shanghai"`) {
			t.Fatalf("kickoff missing the member timezone:\n%s", prompt)
		}
	})

	t.Run("an unset zone is stated, not omitted", func(t *testing.T) {
		prompt := buildMikaOnboardingKickoff("English", "Venus", "", questionnaireAnswers{
			Role: "engineer",
		}, "Hi — welcome to Venus.")
		if !strings.Contains(prompt, "Member IANA timezone: unknown") {
			t.Fatalf("kickoff must say the timezone is unknown:\n%s", prompt)
		}
	})

	// The digest card is clickable whether or not the questionnaire was
	// answered, so the zone must survive the skipped-profile early return.
	t.Run("survives a skipped questionnaire", func(t *testing.T) {
		prompt := buildMikaOnboardingKickoff("English", "Venus", "Europe/Berlin", questionnaireAnswers{
			RoleSkipped:    true,
			UseCaseSkipped: true,
		}, "Hi — welcome to Venus.")
		if !strings.Contains(prompt, `Member IANA timezone: "Europe/Berlin"`) {
			t.Fatalf("skipped profile dropped the timezone:\n%s", prompt)
		}
		if !strings.Contains(prompt, "skipped the profile questions") {
			t.Fatalf("skipped profile lost its neutrality note:\n%s", prompt)
		}
	})
}

func TestNormalizeMessageKindPreservesOnboardingKickoff(t *testing.T) {
	if got := normalizeMessageKind(protocol.ChatMessageKindOnboardingKickoff); got != protocol.ChatMessageKindOnboardingKickoff {
		t.Fatalf("normalizeMessageKind() = %q, want %q", got, protocol.ChatMessageKindOnboardingKickoff)
	}
}

func TestVisibleChatMessagesHidesOnboardingKickoff(t *testing.T) {
	messages := []db.ChatMessage{
		{
			Content:     "internal kickoff",
			MessageKind: protocol.ChatMessageKindOnboardingKickoff,
		},
		{
			Content:     "Hi, I'm Mika.",
			MessageKind: protocol.ChatMessageKindMessage,
		},
	}

	visible := visibleChatMessages(messages)
	if len(visible) != 1 || visible[0].Content != "Hi, I'm Mika." {
		t.Fatalf("visibleChatMessages() = %#v, want only Mika's visible reply", visible)
	}
}

// Every workspace onboards from scratch. The kickoff is built from this
// workspace's own inputs only — nothing about what the member did elsewhere
// reaches the model, so no two workspaces can produce different openings for
// the same profile.
func TestBuildMikaOnboardingKickoffCarriesNoCrossWorkspaceHistory(t *testing.T) {
	prompt := buildMikaOnboardingKickoff("English", "Venus", "Asia/Shanghai", questionnaireAnswers{}, "Hi — welcome to Venus.")

	for _, unwanted := range []string{
		"another workspace",
		"onboarding before",
		"already knows",
		"Keep the introduction to one line",
	} {
		if strings.Contains(prompt, unwanted) {
			t.Fatalf("kickoff leaked cross-workspace history %q:\n%s", unwanted, prompt)
		}
	}
}