diff --git a/README.md b/README.md index 41cff4a..37c14e0 100644 --- a/README.md +++ b/README.md @@ -103,3 +103,25 @@ link-local, CGNAT, multicast and reserved ranges, redirects re-checked hop by ho ## License MIT + +## Money plugins (0.2.0) + +`FINANCE_CATALOG` lists Mercury (business banking), CoinPay (crypto payments) +and moomoo (brokerage and trading); `DEFAULT_PLUGINS` are the ones apps turn +on for every account (Mercury and CoinPay; each person signs in to their own +account). The confirmation gate goes in your tool-call layer: + +```js +import { applyPolicy, describeAction, policyFor, listTools } from "@profullstack/chat-plugins"; + +const policy = policyFor(plugin.url); // by server, not slug +const tools = applyPolicy(policy, await listTools(plugin), plugin.tradingMode ?? "paper"); +// Only allowlisted reads run alone; every other tool (and any unknown name) +// is destructiveHint: true, so needsApproval() stops the loop for the person. +// describeAction(policy, tool, args) is the approval card: amount, currency, +// from, to, memo; or symbol, side, qty, type, prices, estimated cost, paper/live. +``` + +moomoo in paper mode offers reads plus `sim_trade_*` orders only; live order +tools appear only in `"live"` mode, and still ask. `redactLog()` masks tokens, +long digit runs and amounts before anything reaches a log. diff --git a/index.d.ts b/index.d.ts index d341e0b..523b44b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -317,3 +317,44 @@ export function fetchJson( export function parseWwwAuthenticate(header: string): Record; export function protectedResourceCandidates(resource: URL): string[]; export function authServerCandidates(issuer: string): string[]; + +// ── money plugins (src/money.js) ──────────────────────────────────────────── + +export type MoneyKind = "banking" | "crypto" | "trading"; +export type TradingMode = "paper" | "live"; +export interface PluginPolicy { + slug: string; + kind: MoneyKind; + /** Tools that only read. Exact names; anything else asks first. */ + readOnly: ReadonlySet; + /** OAuth scopes to ask for instead of everything the server offers. */ + scopes?: string[]; + /** Brokerage: tools that place, change or cancel PAPER orders. */ + paperWrites?: ReadonlySet; +} +export interface FinanceCatalogEntry { + slug: string; + name: string; + url: string; + description: string; + homepage: string; + icon: string; + /** On for every account (listed and switched on, not signed in). */ + default: boolean; +} +export interface ActionSummary { + title: string; + lines: { label: string; value: string }[]; + account?: TradingMode; + note?: string; +} +export const MODEL_CAN_BE_WRONG: string; +export const FINANCE_CATALOG: readonly FinanceCatalogEntry[]; +export const DEFAULT_PLUGINS: readonly FinanceCatalogEntry[]; +export const POLICIES: Record; +export function policyFor(url: string): PluginPolicy | null; +export function needsConfirmation(policy: PluginPolicy, tool: string): boolean; +export function visibleInMode(policy: PluginPolicy, tool: string, mode: TradingMode): boolean; +export function applyPolicy(policy: PluginPolicy | null, tools: T[], mode?: TradingMode): T[]; +export function describeAction(policy: PluginPolicy | null, tool: string, args: Record): ActionSummary | null; +export function redactLog(v: unknown): string; diff --git a/package.json b/package.json index 6c614ae..c20d0cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@profullstack/chat-plugins", - "version": "0.1.0", + "version": "0.2.0", "type": "module", "description": "Chat plugins = remote MCP servers, with less hoops than connectors: paste a URL or pick from a catalog, sign-in is automatic (OAuth 2.1 + DCR + PKCE), and an agent loop runs the tools even on models with no native tool calling. Zero dependencies.", "keywords": [ diff --git a/src/index.js b/src/index.js index f253d7b..f04a0b2 100644 --- a/src/index.js +++ b/src/index.js @@ -27,3 +27,15 @@ export { toOpenAITools, toolName, buildToolIndex, resolveTool, needsApproval, sl export { runToolLoop, iterateToolLoop, parseToolCall, normalizeReply, toolPrompt, wrapToolResult } from "./loop.js"; export { CATALOG, makeCatalog, resolvePluginInput, uniqueSlug } from "./catalog.js"; export { sealToken, openToken } from "./seal.js"; +export { + MODEL_CAN_BE_WRONG, + FINANCE_CATALOG, + DEFAULT_PLUGINS, + POLICIES, + policyFor, + needsConfirmation, + visibleInMode, + applyPolicy, + describeAction, + redactLog, +} from "./money.js"; diff --git a/src/money.js b/src/money.js new file mode 100644 index 0000000..024501c --- /dev/null +++ b/src/money.js @@ -0,0 +1,372 @@ +// Money plugins: catalog entries for banking (Mercury), crypto payments +// (CoinPay) and brokerage (moomoo), which of them apps turn on by default, +// and the confirmation gate for their tools. +// +// The gate is meant for the app's tool-call layer, not a prompt: for a plugin +// with a policy, a tool may run on its own only when its exact name is on that +// plugin's read-only allowlist. applyPolicy() marks every other tool (a write, +// or a name never seen) destructiveHint: true before the tool loop sees it, so +// needsApproval() is true and the loop stops for the person. A server's own +// annotations cannot loosen it. Policies are chosen by server URL, not slug. +// +// Shared by c0mpute chat and Infernet chat so the two cannot drift. No Node +// imports: describeAction() also runs in the browser. + +export const MODEL_CAN_BE_WRONG = "The model can be wrong. You decide: nothing here runs until you approve it."; +export const FINANCE_CATALOG = Object.freeze([ + { + slug: "mercury", + name: "Mercury (business banking)", + url: "https://mcp.mercury.com/mcp", + description: "Your Mercury accounts in chat: balances, transactions, recipients, cards, statements and cash flow. Payment and transfer requests need your approval here and still go through Mercury's own approval. Sign in with your Mercury account.", + homepage: "https://mercury.com", + icon: "https://mercury.com/favicon.ico", + default: true + }, + { + slug: "coinpay", + name: "CoinPay (crypto payments)", + url: "https://coinpayportal.com/api/mcp", + description: "Your CoinPay business in chat: crypto invoices, receivables aging, projects, contractors and pay runs. Creating anything needs your approval first. Sign in with your CoinPay account.", + homepage: "https://coinpayportal.com", + icon: "https://coinpayportal.com/favicon.ico", + default: true + }, + { + slug: "moomoo", + name: "moomoo (brokerage and trading)", + url: "https://mcp.moomoo.com/mcp", + description: "Quotes, charts, screeners, news and your moomoo positions, balances and orders. Starts on paper trading; switching to live is your call, and every order needs your approval. The model can be wrong and you decide. Sign in with your moomoo account.", + homepage: "https://www.moomoo.com", + icon: "https://www.moomoo.com/favicon.ico", + default: false + } +]); +export const DEFAULT_PLUGINS = FINANCE_CATALOG.filter((e) => e.default); +const MERCURY_READ = new Set([ + "getCurrentDate", + "getAccount", + "getAccounts", + "getAccountCards", + "getAccountStatements", + "getTransaction", + "getTransactionById", + "listTransactions", + "getCashflowSummary", + "getAutoTransferRules", + "listCategories", + "listCredit", + "getCreditAccountStatements", + "getOrganization", + "getRecipient", + "getRecipients", + "getRecipientInvite", + "listRecipientInvites", + "listRecipientsAttachments", + "getTreasury", + "getTreasuryTransactions", + "getInvest", + "getInvestActivity", + "getCard", + "listCards", + "listMerchants", + "getUser", + "getUsers", + "getWebhook", + "getWebhooks", + "listSendMoneyApprovalRequests", + "getSendMoneyApprovalRequest", + "listTransferMoneyApprovalRequests", + "getTransferMoneyApprovalRequest", + "listTasks", + "getTask", + "getSafeRequest", + "getSafeRequests", + "getAttachment", + "getCustomer", + "listCustomers", + "listProducts", + "getInvoice", + "listInvoices", + "listInvoiceAttachments", + "listInvoicePayments", + "listBudgets", + "getBudget", + "getBudgetMembers", + "getBudgetExpenditures", + "listReceipts", + "getReceipt", + "listReimbursements", + "getReimbursement", + "getBooksInstances", + "getBooksSourceAccounts", + "getBooksTransactions", + "getBooksTransaction", + "getBooksLedgers", + "getBooksLedger", + "getBooksLedgerBalances", + "getBooksLineEntries", + "listBooksJournalEntries", + "retrieveBooksJournalEntry", + "getBooksTagGroups", + "getBooksTags", + "getBooksCategorizationRules", + "getBooksProfitAndLoss", + "getBooksBalanceSheet", + "getBooksCashFlow", + "getBooksBills", + "getBooksBill", + "getBooksInvoices", + "getBooksInvoice" +]); +const COINPAY_READ = new Set([ + "invoice_list", + "invoice_get", + "invoice_ar_aging", + "project_list", + "project_get", + "contractor_list", + "contractor_get", + "contractor_work_list", + "pay_run_list", + "pay_run_get", + "document_list", + "document_get" +]); +const MOOMOO_READ = new Set([ + "account_authorized_trd_accs", + "account_funds", + "account_positions", + "account_orders_active", + "account_orders_history", + "account_orders_detail", + "account_order_fills_today", + "account_fills_history", + "account_trading_info", + "sim_trade_account_list", + "sim_trade_cash_info", + "sim_trade_position_list", + "sim_trade_history_order_list", + "sim_trade_max_buy_sell", + "quote_market_snapshot", + "quote_market_state", + "quote_stock_quote", + "quote_stock_feed", + "quote_stock_basicinfo", + "quote_stock_screen", + "quote_option_screen", + "quote_warrant_screen", + "quote_rt_ticker", + "quote_rt_data", + "quote_order_book", + "quote_cur_kline", + "quote_history_kline", + "quote_trading_days", + "quote_top_ten_brokers", + "quote_capital_flow", + "quote_capital_flow_history", + "quote_capital_distribution", + "quote_plate_list", + "quote_plate_stock", + "quote_owner_plate", + "quote_referencefuture_list", + "quote_future_info", + "quote_option_chain", + "quote_option_expiration_date", + "quote_option_volatility", + "quote_option_exercise_probability", + "quote_user_security", + "quote_user_security_group", + "quote_valuation_detail", + "quote_valuation_plate_stock_list", + "quote_valuation_index_stock_plate_list", + "quote_valuation_index_component_stock_list", + "quote_financials_statements", + "quote_financials_revenue_breakdown", + "quote_financials_earnings_price_history", + "quote_company_profile", + "quote_company_executives", + "quote_company_executive_background", + "quote_company_operational_efficiency", + "quote_corporate_actions_dividends", + "quote_corporate_actions_stock_splits", + "quote_corporate_actions_buybacks", + "quote_corporate_actions_rehab", + "quote_shareholders_overview", + "quote_shareholders_institutional", + "quote_shareholders_holding_changes", + "quote_shareholders_holder_detail", + "quote_insider_trade_list", + "quote_insider_holder_list", + "quote_short_interest", + "quote_daily_short_volume", + "quote_research_rating_summary", + "quote_research_analyst_consensus", + "quote_research_morningstar_report", + "quote_news_search", + "quote_community_search", + "quote_economic_calendar_search", + "quote_economic_calendar_hot", + "quote_ipo_list_us", + "quote_ipo_list_hk", + "quote_ipo_list_cn", + "quote_ipo_list_sg", + "quote_ipo_list_my" +]); +const MOOMOO_PAPER_WRITES = new Set(["sim_trade_input_order", "sim_trade_modify_order", "sim_trade_cancel_order"]); +export const POLICIES = { + mercury: { + slug: "mercury", + kind: "banking", + readOnly: MERCURY_READ, + scopes: ["read", "offline_access", "transactions:request", "transfers:request"] + }, + coinpay: { slug: "coinpay", kind: "crypto", readOnly: COINPAY_READ }, + moomoo: { slug: "moomoo", kind: "trading", readOnly: MOOMOO_READ, paperWrites: MOOMOO_PAPER_WRITES } +}; +function parse(u) { + try { + return new URL(u); + } catch { + return null; + } +} +export function policyFor(url) { + const u = parse(url); + if (!u) + return null; + const host = u.hostname.toLowerCase().replace(/\.$/, ""); + if (host === "mcp.mercury.com") + return POLICIES.mercury; + if (host === "mcp.moomoo.com") + return POLICIES.moomoo; + if (host === "coinpayportal.com" || host === "www.coinpayportal.com") + return POLICIES.coinpay; + return null; +} +export function needsConfirmation(policy, tool) { + return !policy.readOnly.has(tool); +} +export function visibleInMode(policy, tool, mode) { + if (policy.kind !== "trading" || mode === "live") + return true; + return policy.readOnly.has(tool) || !!policy.paperWrites?.has(tool); +} +export function applyPolicy(policy, tools, mode = "paper") { + if (!policy) + return tools; + return tools.filter((t) => visibleInMode(policy, t.name, mode)).map((t) => needsConfirmation(policy, t.name) ? { ...t, annotations: { ...t.annotations ?? {}, readOnlyHint: false, destructiveHint: true } } : { ...t, annotations: { ...t.annotations ?? {}, readOnlyHint: true, destructiveHint: false } }); +} +const str = (v) => v === undefined || v === null ? "" : typeof v === "string" ? v : typeof v === "number" || typeof v === "boolean" ? String(v) : JSON.stringify(v); +function pick(args, names) { + const norm = (k) => k.toLowerCase().replace(/[_-]/g, ""); + const want = names.map(norm); + const scan = (o) => { + for (const w of want) { + for (const [k, v] of Object.entries(o)) + if (norm(k) === w && v !== undefined && v !== null && v !== "") + return str(v); + } + return ""; + }; + const top = scan(args); + if (top) + return top; + for (const v of Object.values(args)) { + if (v && typeof v === "object" && !Array.isArray(v)) { + const inner = scan(v); + if (inner) + return inner; + } + } + return ""; +} +function humanTool(tool) { + const s = tool.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/_/g, " ").toLowerCase(); + return s.charAt(0).toUpperCase() + s.slice(1); +} +const num = (s) => { + if (!s) + return null; + const n = Number(String(s).replace(/,/g, "")); + return Number.isFinite(n) ? n : null; +}; +function tradingSummary(tool, args) { + const paper = tool.startsWith("sim_trade_"); + const lines = []; + const add = (label, value) => { + if (value) + lines.push({ label, value }); + }; + const isCancel = /cancel/.test(tool); + const isChange = /modify|replace/.test(tool); + const isPlace = !isCancel && !isChange && /order|place|confirm/.test(tool); + const title = isCancel ? "Cancel order" : isChange ? "Change order" : /confirm/.test(tool) ? "Confirm order" : isPlace ? "Place order" : humanTool(tool); + add("Symbol", pick(args, ["code", "symbol", "ticker", "security"])); + add("Side", pick(args, ["side", "trd_side", "direction"])); + const qty = pick(args, ["qty", "quantity", "new_qty", "shares"]); + add("Quantity", qty); + const type = pick(args, ["order_type", "type"]); + add("Order type", type); + const price = pick(args, ["price", "limit_price", "new_price"]); + add("Limit price", price); + add("Stop / trigger price", pick(args, ["aux_price", "stop_price", "trigger_price"])); + add("Time in force", pick(args, ["time_in_force", "tif"])); + add("Order id", pick(args, ["order_id", "confirm_id"])); + if (isPlace || isChange) { + const q = num(qty); + const p = num(price); + lines.push({ + label: "Estimated cost", + value: q !== null && p !== null && !/MARKET/i.test(type) ? `${(q * p).toLocaleString("en-US", { maximumFractionDigits: 4 })} (quantity x price, before fees; options and futures also multiply by the contract size)` : "at the market price, not known until it fills" + }); + } + add("Account id", pick(args, ["acc_id", "account_id"])); + lines.push({ label: "Account", value: paper ? "PAPER (simulated, no real money)" : "LIVE (real money)" }); + return { title, lines, account: paper ? "paper" : "live", note: MODEL_CAN_BE_WRONG }; +} +function moneySummary(kind, tool, args) { + const lines = []; + const add = (label, value) => { + if (value) + lines.push({ label, value }); + }; + let amount = pick(args, ["amount", "amountUsd", "value", "total", "paymentAmount"]); + let currency = pick(args, ["currency", "currencyCode", "asset", "coin"]); + const items = Array.isArray(args.lineItems) ? args.lineItems : null; + if (!amount && items) { + const t = items.reduce((s, i) => s + (Number(i?.quantity) || 0) * (Number(i?.unit_price) || 0), 0); + amount = `${t.toLocaleString("en-US", { maximumFractionDigits: 8 })} before tax and discount (${items.length} line item${items.length === 1 ? "" : "s"})`; + if (!currency) + currency = "USD"; + } + if (typeof args.minutes === "number") + add("Time", `${args.minutes} minutes`); + add("Amount", amount); + add("Currency", currency); + add("From", pick(args, ["fromAccountId", "sourceAccountId", "from", "accountId", "fromAccount", "businessId", "fromWallet"])); + add("To", pick(args, ["recipientId", "toAccountId", "destinationAccountId", "to", "recipient", "toAddress", "address", "clientId", "contractorId", "projectId"])); + add("Memo", pick(args, ["memo", "note", "notes", "externalMemo", "description", "purpose", "remark"])); + add("Payment method", pick(args, ["paymentMethod", "method", "network", "chain"])); + return { + title: humanTool(tool), + lines, + note: kind === "banking" ? "Mercury payment and transfer requests still need an approver in Mercury." : MODEL_CAN_BE_WRONG + }; +} +export function describeAction(policy, tool, args) { + if (!policy) + return null; + const a = args && typeof args === "object" ? args : {}; + return policy.kind === "trading" ? tradingSummary(tool, a) : moneySummary(policy.kind, tool, a); +} +export function redactLog(v) { + const s = v instanceof Error ? `${v.name}: ${v.message}` : typeof v === "string" ? v : (() => { + try { + return JSON.stringify(v); + } catch { + return String(v); + } + })(); + return s.replace(/(bearer\s+)\S+/gi, "$1[redacted]").replace(/("?(?:access_token|refresh_token|id_token|client_secret|code|code_verifier|token|password|secret)"?\s*[:=]\s*"?)[^"&\s,}]+/gi, "$1[redacted]").replace(/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g, "[jwt]").replace(/[A-Za-z0-9_\-+/=]{32,}/g, "[opaque]").replace(/\d[\d -]{6,}\d/g, "[number]").replace(/[$€£]\s?\d[\d,]*(\.\d+)?/g, "[amount]").slice(0, 500); +} diff --git a/test/money.test.js b/test/money.test.js new file mode 100644 index 0000000..5f2650f --- /dev/null +++ b/test/money.test.js @@ -0,0 +1,117 @@ +import { describe, expect, test } from "bun:test"; +import { applyPolicy, DEFAULT_PLUGINS, describeAction, FINANCE_CATALOG, needsApproval, needsConfirmation, POLICIES, policyFor, redactLog } from "../src/index.js"; + +describe("money plugin catalog", () => { + test("Mercury and CoinPay are on by default; moomoo is listed but off", () => { + expect(DEFAULT_PLUGINS.map((d) => d.slug).sort()).toEqual(["coinpay", "mercury"]); + const moomoo = FINANCE_CATALOG.find((e) => e.slug === "moomoo"); + expect(moomoo.default).toBe(false); + expect(moomoo.description).toContain("you decide"); + for (const e of FINANCE_CATALOG) { + expect(e.url.startsWith("https://")).toBe(true); + expect(e.description.toLowerCase()).not.toContain("first ever"); + } + }); + + test("policy is chosen by server, not by slug", () => { + expect(policyFor("https://mcp.mercury.com/mcp")?.kind).toBe("banking"); + expect(policyFor("https://MCP.mercury.com/mcp/")?.kind).toBe("banking"); + expect(policyFor("https://coinpayportal.com/api/mcp")?.kind).toBe("crypto"); + expect(policyFor("https://mcp.moomoo.com/mcp")?.kind).toBe("trading"); + expect(policyFor("https://threatcrush.com/api/mcp")).toBeNull(); + expect(policyFor("https://mcp.mercury.com.evil.example/mcp")).toBeNull(); + expect(policyFor("not a url")).toBeNull(); + }); +}); + +describe("confirmation gate", () => { + test("only allowlisted reads run alone; writes and unknown tools ask", () => { + const m = POLICIES.mercury; + expect(needsConfirmation(m, "listTransactions")).toBe(false); + expect(needsConfirmation(m, "getAccounts")).toBe(false); + for (const t of ["requestSendMoney", "requestTransferMoney", "approveReimbursement", "createRecipient", "somethingNew"]) expect(needsConfirmation(m, t)).toBe(true); + const c = POLICIES.coinpay; + expect(needsConfirmation(c, "invoice_list")).toBe(false); + for (const t of ["invoice_create_draft", "time_log", "project_invoice_time", "wallet_send"]) expect(needsConfirmation(c, t)).toBe(true); + const mm = POLICIES.moomoo; + expect(needsConfirmation(mm, "quote_stock_quote")).toBe(false); + expect(needsConfirmation(mm, "account_positions")).toBe(false); + for (const t of ["trading_order_place", "trading_order_cancel", "sim_trade_input_order", "quote_modify_user_security", "transfer_funds"]) expect(needsConfirmation(mm, t)).toBe(true); + }); + + test("a server that calls a write tool read-only cannot loosen the rule", () => { + const tools = [ + { name: "listTransactions", annotations: {} }, + { name: "requestSendMoney", annotations: { readOnlyHint: true, destructiveHint: false } }, + { name: "mystery" }, + ]; + const out = applyPolicy(POLICIES.mercury, tools); + expect(out.find((t) => t.name === "listTransactions")?.annotations).toMatchObject({ readOnlyHint: true, destructiveHint: false }); + expect(out.find((t) => t.name === "requestSendMoney")?.annotations).toMatchObject({ readOnlyHint: false, destructiveHint: true }); + expect(out.find((t) => t.name === "mystery")?.annotations).toMatchObject({ destructiveHint: true }); + // The package's own loop check now asks for every non-allowlisted tool. + expect(out.filter((t) => needsApproval(t)).map((t) => t.name)).toEqual(["requestSendMoney", "mystery"]); + // No policy: the server's own annotations stand. + expect(applyPolicy(null, tools)).toBe(tools); + }); + + test("brokerage: paper mode hides live order tools; live shows them (still asking)", () => { + const tools = ["quote_stock_quote", "sim_trade_input_order", "trading_order_place", "trading_order_cancel", "unknown_tool"].map((name) => ({ name })); + const paper = applyPolicy(POLICIES.moomoo, tools, "paper").map((t) => t.name); + expect(paper).toEqual(["quote_stock_quote", "sim_trade_input_order"]); + const live = applyPolicy(POLICIES.moomoo, tools, "live"); + expect(live.map((t) => t.name)).toEqual(tools.map((t) => t.name)); + expect(live.find((t) => t.name === "trading_order_place")?.annotations).toMatchObject({ destructiveHint: true }); + }); +}); + +describe("approval card", () => { + test("a payment shows amount, currency, from, to and memo", () => { + const s = describeAction(POLICIES.mercury, "requestSendMoney", { accountId: "acct_1", recipientId: "rcp_9", amount: 1250.5, currency: "USD", note: "Invoice 42" }); + const get = (l) => s.lines.find((x) => x.label === l)?.value; + expect(s.title).toBe("Request send money"); + expect(get("Amount")).toBe("1250.5"); + expect(get("Currency")).toBe("USD"); + expect(get("From")).toBe("acct_1"); + expect(get("To")).toBe("rcp_9"); + expect(get("Memo")).toBe("Invoice 42"); + expect(s.note).toContain("Mercury"); + }); + + test("a CoinPay draft invoice totals its line items", () => { + const s = describeAction(POLICIES.coinpay, "invoice_create_draft", { clientId: "c1", lineItems: [{ description: "work", quantity: 3, unit_price: 100 }] }); + expect(s.lines.find((x) => x.label === "Amount")?.value).toContain("300"); + expect(s.lines.find((x) => x.label === "Currency")?.value).toBe("USD"); + expect(s.lines.find((x) => x.label === "To")?.value).toBe("c1"); + }); + + test("an order shows symbol, side, quantity, type, prices, estimated cost and paper vs live", () => { + const live = describeAction(POLICIES.moomoo, "trading_order_place", { acc_id: "123", code: "US.AAPL", side: "BUY", qty: "10", order_type: "LIMIT", price: "190.5", time_in_force: "DAY" }); + const get = (l) => live.lines.find((x) => x.label === l)?.value; + expect(live.title).toBe("Place order"); + expect(get("Symbol")).toBe("US.AAPL"); + expect(get("Side")).toBe("BUY"); + expect(get("Quantity")).toBe("10"); + expect(get("Order type")).toBe("LIMIT"); + expect(get("Limit price")).toBe("190.5"); + expect(get("Estimated cost")).toContain("1,905"); + expect(get("Account")).toContain("LIVE"); + expect(live.account).toBe("live"); + expect(live.note).toContain("can be wrong"); + const paper = describeAction(POLICIES.moomoo, "sim_trade_input_order", { code: "US.TSLA", side: "SELL", qty: "1", order_type: "MARKET" }); + expect(paper.account).toBe("paper"); + expect(paper.lines.find((x) => x.label === "Estimated cost")?.value).toContain("market price"); + expect(describeAction(null, "x", {})).toBeNull(); + }); +}); + +describe("logs", () => { + test("tokens, account numbers and amounts are masked", () => { + const out = redactLog(new Error('token exchange failed: {"access_token":"abc.def","refresh_token":"r1"} Bearer eyJhbGciOi.eyJzdWIi.sig acct 1234567890123 paid $1,250.00')); + expect(out).not.toContain("abc.def"); + expect(out).not.toContain("r1\""); + expect(out).not.toContain("1234567890123"); + expect(out).not.toContain("1,250"); + expect(out).toContain("[redacted]"); + }); +});