Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/components/terminal/DeepLinkConfirmModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ vi.mock("@/services/snippetCatalogInstall", () => ({
installCatalogEntries: (...args: unknown[]) => installEntriesMock(...args),
}));

let vaultState = {
selectedVaultIds: ["team-a"],
vaults: [{ id: "team-a", name: "Ops" }, { id: "team-b", name: "Staging" }],
};
vi.mock("@/stores/vaultStore", () => ({
useVaultStore: { getState: () => ({ selectedVaultIds: ["team-a"], vaults: [{ id: "team-a", name: "Ops" }] }) },
useVaultStore: { getState: () => vaultState },
}));

const joinMock = vi.fn(async (..._args: unknown[]) => "local-1");
Expand Down Expand Up @@ -85,6 +89,10 @@ beforeEach(() => {
teamConnections = {};
activeLocalSessionId = null;
snippetEntries = [];
vaultState = {
selectedVaultIds: ["team-a"],
vaults: [{ id: "team-a", name: "Ops" }, { id: "team-b", name: "Staging" }],
};
fetchSnippetCatalogMock.mockClear();
installEntriesMock.mockClear().mockResolvedValue({ imported: 1, errors: 0 });
marketplaceSources = [];
Expand Down Expand Up @@ -236,6 +244,21 @@ test("a snippet-install sheet names the entry and its destination vault before i
);
});

test("a snippet-install writes to the vault the sheet disclosed, not the one selected at accept", async () => {
snippetEntries = [{ id: "docker-cleanup", kind: "pack", name: "Docker cleanup", author: "kevin", snippets: [{}] }];
useDeepLinkStore.setState({ prompt: { route: "snippet-install", entryId: "docker-cleanup" } });
render(<DeepLinkConfirmModal />);
await waitFor(() => expect(screen.getByText("snippets.deepLinkInstall.destination")).toBeTruthy());

// The sheet named "Ops" while it was open; selecting another vault behind it
// must not silently redirect the write it disclosed.
vaultState = { ...vaultState, selectedVaultIds: ["team-b"] };
await userEvent.click(screen.getByText("snippets.deepLinkInstall.action"));

await waitFor(() => expect(installEntriesMock).toHaveBeenCalled());
expect(installEntriesMock.mock.calls[0][1]).toBe("team-a");
});

test("a snippet-install link naming an entry the catalogue does not list cannot be accepted", async () => {
snippetEntries = [];
useDeepLinkStore.setState({ prompt: { route: "snippet-install", entryId: "docker-cleanup" } });
Expand Down
30 changes: 18 additions & 12 deletions src/components/terminal/deepLinkConfirmSpecs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ export interface PluginInstallLoad {
sourceName: string;
}

export interface SnippetInstallLoad {
entry: CatalogEntry;
/** Resolved with the entry, not again at accept: the vault the sheet names is
* then provably the vault written to, however the selection moves while the
* sheet is open. */
vault: { id: string; name: string };
}

/** What each route's `load` produces. `void` for a route with nothing to fetch. */
export interface ConfirmLoad {
join: void;
invite: InviteLoad;
"snippet-install": CatalogEntry;
"snippet-install": SnippetInstallLoad;
"plugin-install": PluginInstallLoad;
}

Expand All @@ -84,11 +92,6 @@ function shareableSessionId(): string | null {
return useTeamSessionStore.getState().connections[id]?.sessionKeyBytes ? id : null;
}

/** The vault an install lands in — read directly since the spec is not a component. */
function installTargetVault(): { id: string; name: string } {
return resolveInstallVault(useVaultStore.getState());
}

export const CONFIRM_SPECS: { [K in ConfirmRoute]: ConfirmSpec<K, ConfirmLoad[K]> } = {
join: {
icon: "lucide:users",
Expand Down Expand Up @@ -149,7 +152,10 @@ export const CONFIRM_SPECS: { [K in ConfirmRoute]: ConfirmSpec<K, ConfirmLoad[K]
// Rejecting here is what leaves accept dead: a sheet that cannot name what
// it would install must never be acceptable.
if (!entry) throw new Error("snippet catalogue entry not found");
return entry;
// Read here rather than in `details` and again in `accept` — the store is
// read directly since the spec is not a component, so two reads are two
// answers whenever the selection moves while the sheet is open.
return { entry, vault: resolveInstallVault(useVaultStore.getState()) };
},
details: (_intent, loaded, t) => ({
title: t("snippets.deepLinkInstall.title"),
Expand All @@ -158,22 +164,22 @@ export const CONFIRM_SPECS: { [K in ConfirmRoute]: ConfirmSpec<K, ConfirmLoad[K]
? // Named on purpose: the install lands in whichever vault is selected, and a
// link the user did not author should not quietly write into one they were
// not thinking about.
t("snippets.deepLinkInstall.destination", { vault: installTargetVault().name })
t("snippets.deepLinkInstall.destination", { vault: loaded.vault.name })
: undefined,
}),
extra: (loaded, t) =>
loaded ? (
<p className="text-sm text-(--t-text-bright)">
{t("snippets.deepLinkInstall.summary", {
name: loaded.name,
author: loaded.author ?? t("snippets.deepLinkInstall.unknownAuthor"),
count: loaded.snippets.length,
name: loaded.entry.name,
author: loaded.entry.author ?? t("snippets.deepLinkInstall.unknownAuthor"),
count: loaded.entry.snippets.length,
})}
</p>
) : null,
accept: async (_intent, loaded) => {
if (!loaded) return;
await installCatalogEntries([{ entry: loaded }], installTargetVault().id);
await installCatalogEntries([{ entry: loaded.entry }], loaded.vault.id);
},
},
"plugin-install": {
Expand Down