A local-first multi-agent app development platform, built on PenguinHarness
/**
* The composer's draft cache (features/chat/draft-cache.ts), kept in localStorage per user
* and Project or Session.
*
* - A stored draft is validated field by field: bad JSON or a non-object reads as an empty
* draft, wrongly typed, unknown or half fields are dropped and the rest kept (the permission
* picks, the paired model references, the AI-prefill mark only as true, the approval mode's
* four values, the skills list); a legacy string model id is always dropped, and so is the
* evaluation mark (`source: "benchmark"`) an earlier release saved with an Evaluation Center
* draft, so the conversation such a draft creates is an ordinary one.
* - Saved drafts read back equal; Project and Session drafts, and two users' drafts of the
* same Project, never read or overwrite each other (#68); a cleared draft reads as empty.
* - Dropping the model pin keeps everything else; dropping the `[default_chat]`-seeded
* selections keeps user content and the model pin, and changes nothing without them.
* - A storage that throws (quota, private mode) never escapes: saving and clearing are
* no-ops and loading is empty.
*/
import { describe, expect, it } from "vitest";
import {
clearDraft,
clearDraftChatDefaults,
clearDraftModelRef,
draftKey,
loadDraft,
parseDraft,
saveDraft,
sessionDraftKey,
} from "../src/features/chat/draft-cache";
import { blockedStorage, memoryStorage } from "./helpers/storage";
describe("parseDraft (field-by-field validation)", () => {
it("keeps the permission picks it recognizes and drops the rest", () => {
expect(
parseDraft(JSON.stringify({ sandbox: { mode: "read-only", network: "sideways" } })).sandbox,
).toEqual({ mode: "read-only" });
expect(parseDraft(JSON.stringify({ sandbox: { mode: "bogus" } })).sandbox).toBeUndefined();
expect(parseDraft(JSON.stringify({ sandbox: "open" })).sandbox).toBeUndefined();
});
it("null / empty string / bad JSON / non-objects all yield an empty draft", () => {
expect(parseDraft(null)).toEqual({});
expect(parseDraft("")).toEqual({});
expect(parseDraft("{not json")).toEqual({});
expect(parseDraft("42")).toEqual({});
expect(parseDraft('"str"')).toEqual({});
expect(parseDraft("null")).toEqual({});
expect(parseDraft("[1,2]")).toEqual({});
});
it("valid fields pass through one by one (both model fields are paired references)", () => {
const raw = JSON.stringify({
text: "Write me a script",
agentId: "default_agent",
workspace: "/srv/repo",
approvalMode: "read-only",
modelRef: { provider: "anthropic", modelId: "claude-opus-4-8" },
handoffAgentId: "agent_helper",
switchModelRef: { provider: "openai", modelId: "gpt-5" },
skills: ["agent-initialization", "penguin-sdk"],
});
expect(parseDraft(raw)).toEqual({
text: "Write me a script",
agentId: "default_agent",
workspace: "/srv/repo",
approvalMode: "read-only",
modelRef: { provider: "anthropic", modelId: "claude-opus-4-8" },
handoffAgentId: "agent_helper",
switchModelRef: { provider: "openai", modelId: "gpt-5" },
skills: ["agent-initialization", "penguin-sdk"],
});
});
it("the AI-prefill mark round-trips only as true, so nothing else can pass for it", () => {
// It decides whether the text is treated as the user's own (draft-view, draft-sessions):
// an "aiPrefill": "yes" left in storage must not read as a composed prompt.
expect(parseDraft(JSON.stringify({ text: "t", aiPrefill: true })).aiPrefill).toBe(true);
expect(parseDraft(JSON.stringify({ text: "t", aiPrefill: "yes" })).aiPrefill).toBeUndefined();
expect(parseDraft(JSON.stringify({ text: "t", aiPrefill: false })).aiPrefill).toBeUndefined();
expect(parseDraft(JSON.stringify({ text: "t" })).aiPrefill).toBeUndefined();
});
it("wrongly typed fields are dropped, the rest kept", () => {
const raw = JSON.stringify({
text: 123,
agentId: null,
workspace: { path: "/x" },
approvalMode: "read-only",
modelRef: ["m"],
handoffAgentId: 7,
switchModelRef: "custom:claude-4-8",
});
expect(parseDraft(raw)).toEqual({ approvalMode: "read-only" });
});
it("legacy string modelId and half references are always dropped (never released, no migration)", () => {
expect(parseDraft(JSON.stringify({ modelId: "claude-opus-4-8" }))).toEqual({});
expect(parseDraft(JSON.stringify({ modelRef: { modelId: "claude-opus-4-8" } }))).toEqual({});
expect(parseDraft(JSON.stringify({ modelRef: { provider: "anthropic" } }))).toEqual({});
});
it("an Evaluation Center draft an earlier release saved reads back as an ordinary one, without its evaluation mark", () => {
// Use -> Evaluate once seeded the slot with its prompt and `source: "benchmark"`, which the
// draft page sent on to file the Session under Evaluations. Read after an upgrade, the draft
// keeps the prompt and creates an ordinary Session.
const draft = {
text: "evaluate",
agentId: "evaluator",
skills: ["agent-evaluation"],
aiPrefill: true,
};
expect(parseDraft(JSON.stringify({ ...draft, source: "benchmark" }))).toEqual(draft);
});
it("the staged /model target validates exactly like modelRef: half references and non-objects are dropped", () => {
// The staged switch chip is cached alongside the text it belongs to (a chip lost while its
// text survived would send that text to the current session on the old model), so a
// corrupted entry must degrade to "no chip" rather than to a half-formed model reference.
expect(parseDraft(JSON.stringify({ switchModelRef: { modelId: "gpt-5" } }))).toEqual({});
expect(parseDraft(JSON.stringify({ switchModelRef: { provider: "openai" } }))).toEqual({});
expect(parseDraft(JSON.stringify({ switchModelRef: null }))).toEqual({});
expect(parseDraft(JSON.stringify({ switchModelRef: 42 }))).toEqual({});
expect(
parseDraft(JSON.stringify({ switchModelRef: { provider: "openai", modelId: 5 } })),
).toEqual({});
// The two model fields are independent: a bad one never takes the good one down with it.
expect(
parseDraft(
JSON.stringify({
modelRef: { provider: "anthropic", modelId: "claude-opus-4-8" },
switchModelRef: { provider: 1, modelId: "gpt-5" },
}),
),
).toEqual({ modelRef: { provider: "anthropic", modelId: "claude-opus-4-8" } });
});
it("approvalMode accepts only the four valid values", () => {
expect(parseDraft(JSON.stringify({ approvalMode: "yolo" }))).toEqual({});
for (const m of ["always-ask", "read-only", "allow-all", "deny-all"]) {
expect(parseDraft(JSON.stringify({ approvalMode: m }))).toEqual({ approvalMode: m });
}
});
it("unknown fields do not pass through", () => {
const out = parseDraft(JSON.stringify({ text: "hi", evil: "x" }));
expect(out).toEqual({ text: "hi" });
});
it("skills: non-arrays dropped, non-string elements filtered out, the whole field dropped when empty after filtering", () => {
// Non-arrays are always discarded
expect(parseDraft(JSON.stringify({ skills: "agent-initialization" }))).toEqual({});
expect(parseDraft(JSON.stringify({ skills: { 0: "x" } }))).toEqual({});
expect(parseDraft(JSON.stringify({ skills: 42 }))).toEqual({});
// Validate each element individually: strings are kept, everything else is filtered out
expect(parseDraft(JSON.stringify({ skills: ["a", 1, null, {}, "b", false] }))).toEqual({
skills: ["a", "b"],
});
// Empty array / empty after filtering -> the whole field is dropped
expect(parseDraft(JSON.stringify({ skills: [] }))).toEqual({});
expect(parseDraft(JSON.stringify({ skills: [7, null] }))).toEqual({});
});
});
describe("load / save / clear (key isolation, errors silenced)", () => {
it("saved drafts read back equal; Project and Session drafts do not affect each other", () => {
const s = memoryStorage();
saveDraft(
draftKey("user-a1", "project-a"),
{
text: "Draft A",
modelRef: { provider: "deepseek", modelId: "deepseek-v4-pro" },
skills: ["agent-initialization"],
},
s,
);
saveDraft(sessionDraftKey("user-a1", "session-1"), { text: "session draft" }, s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({
text: "Draft A",
modelRef: { provider: "deepseek", modelId: "deepseek-v4-pro" },
skills: ["agent-initialization"],
});
expect(loadDraft(sessionDraftKey("user-a1", "session-1"), s)).toEqual({
text: "session draft",
});
// Lock down the key format (e2e accesses it as a literal; ids are all
// <prefix>-<hex> with no ".", so the key space never collides).
expect(draftKey("user-a1", "project-a")).toBe("penguin.chatDraft.user-a1.project-a");
expect(sessionDraftKey("user-a1", "session-1")).toBe(
"penguin.chatDraft.session.user-a1.session-1",
);
expect(s.map.size).toBe(2);
});
it("drafts for the same Project/Session are isolated per user: neither reads nor overwrites the other (#68)", () => {
const s = memoryStorage();
saveDraft(draftKey("user-a1", "project-a"), { text: "A's secret draft" }, s);
saveDraft(sessionDraftKey("user-a1", "session-1"), { text: "A's session draft" }, s);
// Switch accounts (same browser): B reading the same Project/Session only gets an empty draft.
expect(loadDraft(draftKey("user-b2", "project-a"), s)).toEqual({});
expect(loadDraft(sessionDraftKey("user-b2", "session-1"), s)).toEqual({});
// B saves its own draft: coexists with A's, neither overwrites the other.
saveDraft(draftKey("user-b2", "project-a"), { text: "B's draft" }, s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({ text: "A's secret draft" });
expect(loadDraft(draftKey("user-b2", "project-a"), s)).toEqual({ text: "B's draft" });
});
it("reads back an empty draft after clear", () => {
const s = memoryStorage();
saveDraft(draftKey("user-a1", "project-a"), { text: "to be cleared" }, s);
clearDraft(draftKey("user-a1", "project-a"), s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({});
expect(s.map.size).toBe(0);
});
it("clearDraftModelRef drops only the model pin, keeping everything else (default-model change follow-through)", () => {
// Shared by the models page and the project-settings default-model control: after the
// Project default changes, the draft must follow it instead of pinning the old pick.
const s = memoryStorage();
saveDraft(
draftKey("user-a1", "project-a"),
{
text: "keep me",
agentId: "default_agent",
modelRef: { provider: "deepseek", modelId: "deepseek-v4-pro" },
skills: ["agent-initialization"],
},
s,
);
clearDraftModelRef("user-a1", "project-a", s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({
text: "keep me",
agentId: "default_agent",
skills: ["agent-initialization"],
});
// No cached pick (or no draft at all): a no-op, never an errant write.
clearDraftModelRef("user-a1", "project-a", s);
clearDraftModelRef("user-b2", "project-a", s);
expect(s.map.has(draftKey("user-b2", "project-a"))).toBe(false);
// Scoped by user × Project: another user's pin survives.
saveDraft(
draftKey("user-b2", "project-a"),
{ modelRef: { provider: "openai", modelId: "gpt-5" } },
s,
);
clearDraftModelRef("user-a1", "project-a", s);
expect(loadDraft(draftKey("user-b2", "project-a"), s).modelRef).toEqual({
provider: "openai",
modelId: "gpt-5",
});
});
it("clearDraftChatDefaults strips only the [default_chat]-seeded selections, preserving user content and the model pin", () => {
// Called by the project-settings save when the block changed: the next /chat/new must
// reseed Agent / Workspace / approval mode from the fresh defaults, while typed text,
// staged skills, the handoff/switch chips and the switch-becomes-default model
// carry-over (released only by clearDraftModelRef) all survive.
const s = memoryStorage();
saveDraft(
draftKey("user-a1", "project-a"),
{
text: "typed but unsent",
agentId: "old_default_agent",
workspace: "/srv/old-default",
approvalMode: "read-only",
modelRef: { provider: "deepseek", modelId: "deepseek-v4-pro" },
handoffAgentId: "agent_helper",
switchModelRef: { provider: "openai", modelId: "gpt-5" },
skills: ["agent-initialization"],
},
s,
);
clearDraftChatDefaults("user-a1", "project-a", s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({
text: "typed but unsent",
modelRef: { provider: "deepseek", modelId: "deepseek-v4-pro" },
handoffAgentId: "agent_helper",
switchModelRef: { provider: "openai", modelId: "gpt-5" },
skills: ["agent-initialization"],
});
});
it('clearDraftChatDefaults strips any subset of the three fields (a cached "" workspace counts) and is otherwise a no-op', () => {
const s = memoryStorage();
// A single seeded field is enough to rewrite; "" workspace is an explicit "auto temp"
// pin and must be stripped like any other value (undefined-check, not truthiness).
saveDraft(draftKey("user-a1", "project-a"), { text: "t", workspace: "" }, s);
clearDraftChatDefaults("user-a1", "project-a", s);
expect(loadDraft(draftKey("user-a1", "project-a"), s)).toEqual({ text: "t" });
// Nothing seeded left (or no draft at all): a no-op, never an errant write.
clearDraftChatDefaults("user-a1", "project-a", s);
clearDraftChatDefaults("user-b2", "project-a", s);
expect(s.map.has(draftKey("user-b2", "project-a"))).toBe(false);
// Scoped by user × Project: another user's pins survive.
saveDraft(draftKey("user-b2", "project-a"), { agentId: "default_agent" }, s);
clearDraftChatDefaults("user-a1", "project-a", s);
expect(loadDraft(draftKey("user-b2", "project-a"), s)).toEqual({ agentId: "default_agent" });
});
it("storage throwing (quota/private mode): save does not throw, load yields an empty draft", () => {
const broken = blockedStorage();
expect(() => saveDraft(draftKey("u", "p"), { text: "x" }, broken)).not.toThrow();
expect(() => clearDraft(draftKey("u", "p"), broken)).not.toThrow();
expect(loadDraft(draftKey("u", "p"), broken)).toEqual({});
});
});