@camposcloud/cli 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -2
- package/dist/commands/apps.js +197 -0
- package/dist/commands/auth.js +194 -0
- package/dist/commands/completion.js +94 -0
- package/dist/commands/create.js +220 -0
- package/dist/commands/dashboard.js +44 -0
- package/dist/commands/db.js +85 -0
- package/dist/commands/deploy.js +104 -0
- package/dist/commands/env.js +103 -0
- package/dist/commands/files.js +101 -0
- package/dist/commands/lang.js +34 -0
- package/dist/commands/manage.js +121 -0
- package/dist/commands/teams.js +127 -0
- package/dist/commands/utils.js +102 -0
- package/dist/core/api.js +157 -0
- package/dist/core/apiErrors.js +38 -0
- package/dist/core/apps.js +98 -0
- package/dist/core/config.js +34 -0
- package/dist/core/context.js +43 -0
- package/dist/core/credentials.js +90 -0
- package/dist/core/databases.js +53 -0
- package/dist/core/detect.js +75 -0
- package/dist/core/deviceLogin.js +49 -0
- package/dist/core/env.js +71 -0
- package/dist/core/files.js +51 -0
- package/dist/core/help.js +193 -0
- package/dist/core/i18n.js +581 -0
- package/dist/core/logs.js +87 -0
- package/dist/core/output.js +27 -0
- package/dist/core/project.js +119 -0
- package/dist/core/prompt.js +29 -0
- package/dist/core/session.js +21 -0
- package/dist/core/suggest.js +28 -0
- package/dist/core/teams.js +39 -0
- package/dist/core/update.js +48 -0
- package/dist/index.js +223 -0
- package/dist/tui/App.js +400 -0
- package/dist/tui/App.test.js +189 -0
- package/dist/tui/Picker.js +87 -0
- package/dist/tui/clickable.js +64 -0
- package/dist/tui/mouse.js +65 -0
- package/dist/tui/run.js +27 -0
- package/package.json +57 -4
package/dist/tui/App.js
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { Box, Text, useApp, useInput, useWindowSize } from "ink";
|
|
4
|
+
import { ApiError } from "../core/api.js";
|
|
5
|
+
import { appAddress, appState, cpuLabel, formatBytes, formatMB, getApp, listApps, memoryLabel, memoryShare, nodeName, powerApp, runtimeLabel } from "../core/apps.js";
|
|
6
|
+
import { connectionUrl, dbState, dbTypeLabel, listDbs, powerDb } from "../core/databases.js";
|
|
7
|
+
import { storeTeam } from "../core/context.js";
|
|
8
|
+
import { maskValue, readEnv } from "../core/env.js";
|
|
9
|
+
import { listTeams } from "../core/teams.js";
|
|
10
|
+
import { t } from "../core/i18n.js";
|
|
11
|
+
import { localizeServerMessage } from "../core/apiErrors.js";
|
|
12
|
+
import { streamLogs } from "../core/logs.js";
|
|
13
|
+
import { Clickable, useWheel } from "./clickable.js";
|
|
14
|
+
/**
|
|
15
|
+
* `campos` with nothing after it: the dashboard in the terminal. Everything works with the arrow keys alone:
|
|
16
|
+
* ← → change tab, ↑ ↓ choose, Enter opens or runs, Esc goes back. Letters are shortcuts, and every row, tab and
|
|
17
|
+
* menu item can also be clicked.
|
|
18
|
+
*/
|
|
19
|
+
const BLUE = "#4f86ff";
|
|
20
|
+
const GREEN = "#3ecf8e";
|
|
21
|
+
const RED = "#f2555a";
|
|
22
|
+
const YELLOW = "#f5b544";
|
|
23
|
+
const MUTED = "#8d8d93";
|
|
24
|
+
const FAINT = "#5f5f66";
|
|
25
|
+
const SELECTED_BG = "#1c1c20";
|
|
26
|
+
const STATE_COLOR = { online: GREEN, offline: FAINT, suspended: RED, creating: YELLOW };
|
|
27
|
+
const REFRESH_MS = 5000;
|
|
28
|
+
const TABS = ["apps", "databases", "account"];
|
|
29
|
+
function errorText(error) {
|
|
30
|
+
if (error instanceof ApiError)
|
|
31
|
+
return localizeServerMessage(error.message);
|
|
32
|
+
return error?.message ?? String(error);
|
|
33
|
+
}
|
|
34
|
+
/** Restarting something that is not running means starting it (the worker refuses to restart a stopped container). */
|
|
35
|
+
const effectiveAction = (action, state) => (action === "restart" && state !== "online" ? "start" : action);
|
|
36
|
+
export function Dashboard({ client, me, apiUrl, via, team: initialTeam, openUrl, siteUrl, initialAppId }) {
|
|
37
|
+
const { exit } = useApp();
|
|
38
|
+
const { rows } = useWindowSize();
|
|
39
|
+
// One line short of the window: a full-height frame scrolls the screen on some Windows terminals.
|
|
40
|
+
const height = Math.max(12, rows - 1);
|
|
41
|
+
const [tab, setTab] = useState("apps");
|
|
42
|
+
const [view, setView] = useState(initialAppId ? { kind: "detail", id: initialAppId } : { kind: "list" });
|
|
43
|
+
const [apps, setApps] = useState(null);
|
|
44
|
+
const [dbs, setDbs] = useState(null);
|
|
45
|
+
const [loadError, setLoadError] = useState(null);
|
|
46
|
+
const [selected, setSelected] = useState({ apps: 0, databases: 0, account: 0 });
|
|
47
|
+
const [notice, setNotice] = useState(null);
|
|
48
|
+
const [busy, setBusy] = useState(null);
|
|
49
|
+
const [team, setTeam] = useState(initialTeam);
|
|
50
|
+
const refresh = useCallback(async () => {
|
|
51
|
+
try {
|
|
52
|
+
const [appList, dbList] = await Promise.all([listApps(client, team?.id), listDbs(client, team?.id).catch(() => null)]);
|
|
53
|
+
setApps(appList.apps);
|
|
54
|
+
if (dbList)
|
|
55
|
+
setDbs(dbList);
|
|
56
|
+
setLoadError(null);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
setLoadError(errorText(error));
|
|
60
|
+
}
|
|
61
|
+
}, [client, team?.id]);
|
|
62
|
+
// The lists keep themselves current while the dashboard is open (not while the console streams).
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
refresh();
|
|
65
|
+
const timer = setInterval(() => { if (view.kind !== "logs")
|
|
66
|
+
refresh(); }, REFRESH_MS);
|
|
67
|
+
return () => clearInterval(timer);
|
|
68
|
+
}, [refresh, view.kind]);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!notice)
|
|
71
|
+
return;
|
|
72
|
+
const timer = setTimeout(() => setNotice(null), 5000);
|
|
73
|
+
return () => clearTimeout(timer);
|
|
74
|
+
}, [notice]);
|
|
75
|
+
const items = tab === "apps" ? apps ?? [] : tab === "databases" ? dbs ?? [] : [];
|
|
76
|
+
const index = Math.min(selected[tab], Math.max(0, items.length - 1));
|
|
77
|
+
const choose = (next) => setSelected((current) => ({ ...current, [tab]: Math.max(0, Math.min(items.length - 1, next)) }));
|
|
78
|
+
const power = useCallback(async (kind, target, action, state) => {
|
|
79
|
+
if (busy)
|
|
80
|
+
return;
|
|
81
|
+
const real = effectiveAction(action, state);
|
|
82
|
+
setBusy(target._id);
|
|
83
|
+
setNotice({ tone: "info", text: t(`apps.power.${real}.doing`, { name: target.name }) });
|
|
84
|
+
try {
|
|
85
|
+
if (kind === "app")
|
|
86
|
+
await powerApp(client, target._id, real);
|
|
87
|
+
else
|
|
88
|
+
await powerDb(client, target._id, real);
|
|
89
|
+
setNotice({ tone: "ok", text: t(`apps.power.${real}.done`, { name: target.name }) });
|
|
90
|
+
await refresh();
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
setNotice({ tone: "error", text: errorText(error) });
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
setBusy(null);
|
|
97
|
+
}
|
|
98
|
+
}, [busy, client, refresh]);
|
|
99
|
+
const switchTab = (next) => {
|
|
100
|
+
setTab(next);
|
|
101
|
+
setView({ kind: "list" });
|
|
102
|
+
};
|
|
103
|
+
const chooseTeam = (next) => {
|
|
104
|
+
setTeam(next);
|
|
105
|
+
storeTeam(apiUrl, next);
|
|
106
|
+
setApps(null);
|
|
107
|
+
setDbs(null);
|
|
108
|
+
setSelected({ apps: 0, databases: 0, account: 0 });
|
|
109
|
+
setView({ kind: "list" });
|
|
110
|
+
setNotice({ tone: "ok", text: next ? t("team.switched", { team: next.name }) : t("team.switchedPersonal") });
|
|
111
|
+
};
|
|
112
|
+
// Keys of the list level; the detail and the console handle their own.
|
|
113
|
+
useInput((input, key) => {
|
|
114
|
+
if (view.kind !== "list")
|
|
115
|
+
return;
|
|
116
|
+
if (input === "q" || key.escape)
|
|
117
|
+
return exit();
|
|
118
|
+
if (input === "t")
|
|
119
|
+
return setView({ kind: "team" });
|
|
120
|
+
const position = TABS.indexOf(tab);
|
|
121
|
+
if (key.rightArrow || key.tab)
|
|
122
|
+
return switchTab(TABS[(position + 1) % TABS.length]);
|
|
123
|
+
if (key.leftArrow)
|
|
124
|
+
return switchTab(TABS[(position + TABS.length - 1) % TABS.length]);
|
|
125
|
+
if (input === "1" || input === "2" || input === "3")
|
|
126
|
+
return switchTab(TABS[Number(input) - 1]);
|
|
127
|
+
if (key.upArrow || input === "k")
|
|
128
|
+
choose(index - 1);
|
|
129
|
+
else if (key.downArrow || input === "j")
|
|
130
|
+
choose(index + 1);
|
|
131
|
+
else if (key.pageUp)
|
|
132
|
+
choose(index - 10);
|
|
133
|
+
else if (key.pageDown)
|
|
134
|
+
choose(index + 10);
|
|
135
|
+
else if (key.return && items[index])
|
|
136
|
+
setView({ kind: "detail", id: items[index]._id });
|
|
137
|
+
// Letter shortcuts for applications: r restarts (starts, when stopped), l opens the console.
|
|
138
|
+
else if (tab === "apps" && apps?.[index] && input === "r")
|
|
139
|
+
power("app", apps[index], "restart", appState(apps[index]));
|
|
140
|
+
else if (tab === "apps" && apps?.[index] && input === "l")
|
|
141
|
+
setView({ kind: "logs", id: apps[index]._id });
|
|
142
|
+
});
|
|
143
|
+
useWheel((direction) => {
|
|
144
|
+
if (view.kind === "list")
|
|
145
|
+
choose(index + direction);
|
|
146
|
+
});
|
|
147
|
+
const app = (view.kind === "detail" || view.kind === "logs" || view.kind === "env") && tab === "apps" ? apps?.find((item) => item._id === view.id) : undefined;
|
|
148
|
+
const db = view.kind === "detail" && tab === "databases" ? dbs?.find((item) => item._id === view.id) : undefined;
|
|
149
|
+
const back = () => setView({ kind: "list" });
|
|
150
|
+
const loading = (tab === "apps" && apps === null) || (tab === "databases" && dbs === null);
|
|
151
|
+
return (_jsxs(Box, { flexDirection: "column", height: height, paddingX: 1, children: [_jsx(Header, { tab: tab, email: me.email, team: team, onTab: switchTab, onTeam: () => setView({ kind: "team" }) }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, marginTop: 1, children: [view.kind === "team" && _jsx(TeamPicker, { client: client, current: team, onChoose: chooseTeam, onBack: () => setView({ kind: "list" }) }), view.kind !== "team" && tab === "account" && _jsx(Account, { me: me, apiUrl: apiUrl, via: via, team: team }), tab !== "account" && loading && (loadError ? _jsx(Text, { color: RED, children: loadError }) : _jsx(Text, { color: MUTED, children: t("tui.loading") })), tab === "apps" && apps && view.kind === "list" && (_jsx(AppList, { apps: apps, selected: index, busy: busy, maxRows: Math.max(3, height - 9), onSelect: (next) => (next === index ? setView({ kind: "detail", id: apps[next]._id }) : choose(next)) })), tab === "apps" && view.kind === "detail" && app && (_jsx(AppDetail, { app: app, client: client, busy: busy === app._id, onBack: back, onLogs: () => setView({ kind: "logs", id: app._id }), onEnv: () => setView({ kind: "env", id: app._id }), onOpen: openUrl && siteUrl ? () => openUrl(`${siteUrl}/dashboard/applications/${app._id}${team ? `?team=${team.id}` : ""}`) : undefined, onSite: openUrl && appAddress(app) ? () => openUrl(`https://${appAddress(app)}`) : undefined, onPower: (action) => power("app", app, action, appState(app)) })), tab === "apps" && view.kind === "env" && app && (_jsx(EnvView, { app: app, client: client, onBack: () => setView({ kind: "detail", id: app._id }) })), tab === "apps" && view.kind === "logs" && app && (_jsx(Console, { app: app, client: client, height: height - 6, onBack: () => setView({ kind: "detail", id: app._id }) })), tab === "databases" && dbs && view.kind === "list" && (_jsx(DbList, { dbs: dbs, selected: index, busy: busy, maxRows: Math.max(3, height - 9), onSelect: (next) => (next === index ? setView({ kind: "detail", id: dbs[next]._id }) : choose(next)) })), tab === "databases" && view.kind === "detail" && db && (_jsx(DbDetail, { db: db, busy: busy === db._id, onBack: back, onPower: (action) => power("db", db, action, dbState(db)) }))] }), notice ? (_jsxs(Text, { color: notice.tone === "ok" ? GREEN : notice.tone === "error" ? RED : MUTED, wrap: "truncate-end", children: [notice.tone === "ok" ? "✓ " : notice.tone === "error" ? "✗ " : "… ", notice.text] })) : (_jsx(Text, { color: FAINT, wrap: "truncate-end", children: view.kind === "team" ? t("picker.hint") : view.kind === "env" ? t("tui.env.hint") : tab === "account" ? t("tui.hint.account") : view.kind === "list" ? t("tui.hint.list") : view.kind === "detail" ? t("tui.hint.detail") : t("tui.hint.logs") }))] }));
|
|
152
|
+
}
|
|
153
|
+
function Header({ tab, email, team, onTab, onTeam }) {
|
|
154
|
+
const labels = { apps: t("tui.tab.apps"), databases: t("tui.tab.databases"), account: t("tui.tab.account") };
|
|
155
|
+
return (_jsxs(Box, { justifyContent: "space-between", children: [_jsxs(Box, { gap: 3, children: [_jsx(Text, { bold: true, children: "Campos Cloud" }), _jsx(Box, { gap: 2, children: TABS.map((id) => (_jsx(Clickable, { onClick: () => onTab(id), children: _jsx(Text, { color: tab === id ? "white" : MUTED, bold: tab === id, underline: tab === id, children: labels[id] }) }, id))) })] }), _jsxs(Box, { gap: 2, children: [_jsx(Clickable, { onClick: onTeam, children: _jsxs(Text, { color: MUTED, children: [t("tui.team"), ": ", _jsx(Text, { color: "white", bold: true, children: team ? team.name : t("tui.teamPersonal") }), " ", _jsx(Text, { color: FAINT, children: "(t)" })] }) }), _jsx(Text, { color: FAINT, children: email })] })] }));
|
|
156
|
+
}
|
|
157
|
+
/** The personal account and the teams, to choose where the dashboard works. */
|
|
158
|
+
function TeamPicker({ client, current, onChoose, onBack }) {
|
|
159
|
+
const [teams, setTeams] = useState(null);
|
|
160
|
+
const [error, setError] = useState(null);
|
|
161
|
+
useEffect(() => { listTeams(client).then(setTeams).catch((reason) => setError(errorText(reason))); }, [client]);
|
|
162
|
+
if (error)
|
|
163
|
+
return _jsx(Text, { color: RED, children: error });
|
|
164
|
+
if (!teams)
|
|
165
|
+
return _jsx(Text, { color: MUTED, children: t("tui.loading") });
|
|
166
|
+
const PERSONAL = "__personal__";
|
|
167
|
+
const items = [
|
|
168
|
+
{ id: PERSONAL, label: `${t("team.personal")}${!current ? ` · ${t("team.active")}` : ""}` },
|
|
169
|
+
...teams.map((team) => ({ id: team._id, label: `${team.name}${current?.id === team._id ? ` · ${t("team.active")}` : ""}` })),
|
|
170
|
+
];
|
|
171
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: t("tui.chooseTeam") }), _jsx(Box, { marginTop: 1, children: _jsx(Menu, { items: items, onBack: onBack, onRun: (id) => {
|
|
172
|
+
if (id === PERSONAL)
|
|
173
|
+
return onChoose(null);
|
|
174
|
+
const chosen = teams.find((team) => team._id === id);
|
|
175
|
+
if (chosen)
|
|
176
|
+
onChoose({ id: chosen._id, name: chosen.name });
|
|
177
|
+
} }) })] }));
|
|
178
|
+
}
|
|
179
|
+
function Menu({ items, onRun, onBack, active = true }) {
|
|
180
|
+
const [focus, setFocus] = useState(0);
|
|
181
|
+
const current = Math.min(focus, items.length - 1);
|
|
182
|
+
useInput((_, key) => {
|
|
183
|
+
if (key.upArrow)
|
|
184
|
+
setFocus((value) => (Math.min(value, items.length - 1) + items.length - 1) % items.length);
|
|
185
|
+
else if (key.downArrow)
|
|
186
|
+
setFocus((value) => (Math.min(value, items.length - 1) + 1) % items.length);
|
|
187
|
+
else if (key.return && !items[current]?.disabled)
|
|
188
|
+
onRun(items[current].id);
|
|
189
|
+
else if ((key.escape || key.leftArrow || key.backspace) && onBack)
|
|
190
|
+
onBack();
|
|
191
|
+
}, { isActive: active });
|
|
192
|
+
return (_jsx(Box, { flexDirection: "column", children: items.map((item, position) => {
|
|
193
|
+
const isFocused = position === current;
|
|
194
|
+
const color = item.disabled ? FAINT : item.tone === "danger" ? RED : "white";
|
|
195
|
+
return (_jsx(Clickable, { onClick: () => { setFocus(position); if (!item.disabled)
|
|
196
|
+
onRun(item.id); }, children: _jsxs(Text, { backgroundColor: isFocused && active ? SELECTED_BG : undefined, children: [_jsx(Text, { color: BLUE, children: isFocused && active ? "› " : " " }), _jsx(Text, { color: color, bold: isFocused && active, children: item.label }), _jsx(Text, { children: " " })] }) }, item.id));
|
|
197
|
+
}) }));
|
|
198
|
+
}
|
|
199
|
+
/** A bar of `width` cells, filled to `share` (0 to 1). */
|
|
200
|
+
function Meter({ share, width = 16 }) {
|
|
201
|
+
const filled = share === null ? 0 : Math.max(share > 0 ? 1 : 0, Math.round(share * width));
|
|
202
|
+
const color = share !== null && share > 0.9 ? RED : share !== null && share > 0.7 ? YELLOW : BLUE;
|
|
203
|
+
return (_jsxs(Text, { children: [_jsx(Text, { color: color, children: "━".repeat(filled) }), _jsx(Text, { color: "#2a2a2e", children: "━".repeat(width - filled) })] }));
|
|
204
|
+
}
|
|
205
|
+
const fit = (text, width) => (text.length > width - 1 ? `${text.slice(0, width - 2)}…` : text).padEnd(width);
|
|
206
|
+
/** Rows that keep the selected one in view when the list is taller than the screen. */
|
|
207
|
+
function windowOf(list, selected, maxRows) {
|
|
208
|
+
const start = Math.max(0, Math.min(selected - Math.floor(maxRows / 2), list.length - maxRows));
|
|
209
|
+
return { start, rows: list.slice(start, start + maxRows) };
|
|
210
|
+
}
|
|
211
|
+
function Row({ selected, onClick, children }) {
|
|
212
|
+
return (_jsx(Clickable, { onClick: onClick, children: _jsxs(Text, { backgroundColor: selected ? SELECTED_BG : undefined, wrap: "truncate-end", children: [_jsx(Text, { color: selected ? BLUE : SELECTED_BG, children: selected ? "▍ " : " " }), children] }) }));
|
|
213
|
+
}
|
|
214
|
+
const APP_COLUMNS = { name: 26, state: 13, memory: 20, cpu: 7 };
|
|
215
|
+
function AppList({ apps, selected, busy, maxRows, onSelect }) {
|
|
216
|
+
if (apps.length === 0)
|
|
217
|
+
return _jsx(Text, { color: MUTED, children: t("apps.none") });
|
|
218
|
+
const { start, rows } = windowOf(apps, selected, maxRows);
|
|
219
|
+
const totalMB = apps.reduce((sum, app) => sum + (appState(app) === "online" ? app.allocatedMemoryMB ?? 0 : 0), 0);
|
|
220
|
+
const online = apps.filter((app) => appState(app) === "online").length;
|
|
221
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: MUTED, children: t("tui.summary", { count: String(apps.length), online: String(online), memory: formatMB(totalMB) }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: FAINT, children: [" ", fit(t("apps.col.name"), APP_COLUMNS.name), fit(t("apps.col.state"), APP_COLUMNS.state), fit(t("apps.col.memory"), APP_COLUMNS.memory), fit(t("apps.col.cpu"), APP_COLUMNS.cpu), t("apps.col.runtime")] }) }), rows.map((app, offset) => {
|
|
222
|
+
const position = start + offset;
|
|
223
|
+
const state = appState(app);
|
|
224
|
+
return (_jsxs(Row, { selected: position === selected, onClick: () => onSelect(position), children: [_jsx(Text, { bold: position === selected, color: "white", children: fit(app.name, APP_COLUMNS.name) }), _jsx(Text, { color: STATE_COLOR[state], children: fit(busy === app._id ? "◌ …" : `● ${t(`state.${state}`)}`, APP_COLUMNS.state) }), _jsx(Text, { children: fit(memoryLabel(app), APP_COLUMNS.memory) }), _jsx(Text, { color: MUTED, children: fit(cpuLabel(app), APP_COLUMNS.cpu) }), _jsx(Text, { color: MUTED, children: runtimeLabel(app) })] }, app._id));
|
|
225
|
+
}), apps.length > maxRows && _jsxs(Text, { color: FAINT, children: [" ", t("tui.more", { shown: String(rows.length), total: String(apps.length) })] })] }));
|
|
226
|
+
}
|
|
227
|
+
function InfoRows({ rows }) {
|
|
228
|
+
return (_jsx(Box, { flexDirection: "column", children: rows.map(([label, value]) => (_jsxs(Box, { children: [_jsx(Box, { width: 16, flexShrink: 0, children: _jsx(Text, { color: MUTED, children: label }) }), _jsx(Text, { wrap: "truncate-end", children: value })] }, label))) }));
|
|
229
|
+
}
|
|
230
|
+
function Breadcrumb({ parent, name, onBack }) {
|
|
231
|
+
return (_jsxs(Box, { gap: 1, children: [_jsx(Clickable, { onClick: onBack, children: _jsxs(Text, { color: MUTED, children: ["\u2190 ", parent] }) }), _jsx(Text, { color: FAINT, children: "/" }), _jsx(Text, { bold: true, children: name })] }));
|
|
232
|
+
}
|
|
233
|
+
/** The menu of a running or stopped thing; stopping asks first, in the same menu. */
|
|
234
|
+
function PowerMenu({ state, busy, extra, onPower, onExtra, onBack }) {
|
|
235
|
+
const [confirming, setConfirming] = useState(false);
|
|
236
|
+
const online = state === "online";
|
|
237
|
+
if (confirming) {
|
|
238
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: YELLOW, children: t("tui.confirmStopQuestion") }), _jsx(Menu, { items: [{ id: "yes", label: t("tui.confirmStopYes"), tone: "danger" }, { id: "no", label: t("tui.cancel") }], onRun: (id) => { setConfirming(false); if (id === "yes")
|
|
239
|
+
onPower("stop"); }, onBack: () => setConfirming(false) })] }));
|
|
240
|
+
}
|
|
241
|
+
const items = [
|
|
242
|
+
...(online ? [{ id: "restart", label: t("tui.action.restart"), disabled: busy }] : [{ id: "start", label: t("tui.action.start"), disabled: busy }]),
|
|
243
|
+
...(extra ?? []),
|
|
244
|
+
...(online ? [{ id: "stop", label: t("tui.action.stop"), tone: "danger", disabled: busy }] : []),
|
|
245
|
+
{ id: "back", label: t("tui.back") },
|
|
246
|
+
];
|
|
247
|
+
return (_jsx(Menu, { items: items, onBack: onBack, onRun: (id) => {
|
|
248
|
+
if (id === "back")
|
|
249
|
+
onBack();
|
|
250
|
+
else if (id === "stop")
|
|
251
|
+
setConfirming(true);
|
|
252
|
+
else if (id === "start" || id === "restart")
|
|
253
|
+
onPower(id);
|
|
254
|
+
else
|
|
255
|
+
onExtra?.(id);
|
|
256
|
+
} }));
|
|
257
|
+
}
|
|
258
|
+
function AppDetail({ app, client, busy, onBack, onLogs, onEnv, onOpen, onSite, onPower }) {
|
|
259
|
+
const [full, setFull] = useState(null);
|
|
260
|
+
useEffect(() => { getApp(client, app._id).then(setFull).catch(() => undefined); }, [client, app._id]);
|
|
261
|
+
const data = { ...full, ...app, workerNodeId: full?.workerNodeId ?? app.workerNodeId };
|
|
262
|
+
const state = appState(data);
|
|
263
|
+
// Shortcuts also work here.
|
|
264
|
+
useInput((input) => {
|
|
265
|
+
if (input === "l")
|
|
266
|
+
onLogs();
|
|
267
|
+
else if (input === "r" && !busy)
|
|
268
|
+
onPower("restart");
|
|
269
|
+
});
|
|
270
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Breadcrumb, { parent: t("tui.tab.apps"), name: data.name, onBack: onBack }), _jsxs(Box, { marginTop: 1, gap: 3, children: [_jsxs(Text, { color: STATE_COLOR[state], children: ["\u25CF ", busy ? "…" : t(`state.${state}`)] }), _jsxs(Box, { gap: 1, children: [_jsx(Text, { color: MUTED, children: t("apps.info.memory") }), _jsx(Meter, { share: memoryShare(data) }), _jsx(Text, { children: memoryLabel(data) })] })] }), data.restartRequired && _jsx(Text, { color: YELLOW, children: t("apps.info.restartRequired") }), _jsx(Box, { marginTop: 1, children: _jsx(InfoRows, { rows: [
|
|
271
|
+
[t("apps.info.runtime"), runtimeLabel(data) || "—"],
|
|
272
|
+
[t("apps.info.cpu"), `${cpuLabel(data)} · ${data.allocatedVCpu ?? "?"} vCPU`],
|
|
273
|
+
[t("apps.info.address"), appAddress(data) || "—"],
|
|
274
|
+
[t("apps.info.node"), nodeName(data) || "—"],
|
|
275
|
+
[t("apps.info.id"), data._id],
|
|
276
|
+
] }) }), _jsx(Box, { marginTop: 1, children: _jsx(PowerMenu, { state: state, busy: busy, extra: [
|
|
277
|
+
{ id: "logs", label: t("tui.action.logs") },
|
|
278
|
+
{ id: "env", label: t("tui.action.env") },
|
|
279
|
+
...(onSite ? [{ id: "site", label: t("tui.action.site") }] : []),
|
|
280
|
+
...(onOpen ? [{ id: "open", label: t("tui.action.open") }] : []),
|
|
281
|
+
], onPower: onPower, onExtra: (id) => (id === "logs" ? onLogs() : id === "env" ? onEnv() : id === "site" ? onSite?.() : id === "open" ? onOpen?.() : undefined), onBack: onBack }) })] }));
|
|
282
|
+
}
|
|
283
|
+
const DB_COLUMNS = { name: 24, type: 13, state: 13, memory: 10 };
|
|
284
|
+
function DbList({ dbs, selected, busy, maxRows, onSelect }) {
|
|
285
|
+
if (dbs.length === 0)
|
|
286
|
+
return _jsx(Text, { color: MUTED, children: t("db.none") });
|
|
287
|
+
const { start, rows } = windowOf(dbs, selected, maxRows);
|
|
288
|
+
const online = dbs.filter((db) => dbState(db) === "online").length;
|
|
289
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: MUTED, children: t("tui.dbSummary", { count: String(dbs.length), online: String(online) }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: FAINT, children: [" ", fit(t("apps.col.name"), DB_COLUMNS.name), fit(t("db.col.type"), DB_COLUMNS.type), fit(t("apps.col.state"), DB_COLUMNS.state), fit(t("apps.col.memory"), DB_COLUMNS.memory), t("db.col.address")] }) }), rows.map((db, offset) => {
|
|
290
|
+
const position = start + offset;
|
|
291
|
+
const state = dbState(db);
|
|
292
|
+
return (_jsxs(Row, { selected: position === selected, onClick: () => onSelect(position), children: [_jsx(Text, { bold: position === selected, color: "white", children: fit(db.name, DB_COLUMNS.name) }), _jsx(Text, { color: MUTED, children: fit(dbTypeLabel(db), DB_COLUMNS.type) }), _jsx(Text, { color: STATE_COLOR[state], children: fit(busy === db._id ? "◌ …" : `● ${t(`state.${state}`)}`, DB_COLUMNS.state) }), _jsx(Text, { children: fit(formatMB(db.allocatedMemoryMB), DB_COLUMNS.memory) }), _jsx(Text, { color: MUTED, children: db.connection?.port ? `${db.connection.host}:${db.connection.port}` : "—" })] }, db._id));
|
|
293
|
+
})] }));
|
|
294
|
+
}
|
|
295
|
+
function DbDetail({ db, busy, onBack, onPower }) {
|
|
296
|
+
const state = dbState(db);
|
|
297
|
+
const url = connectionUrl(db);
|
|
298
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Breadcrumb, { parent: t("tui.tab.databases"), name: db.name, onBack: onBack }), _jsxs(Box, { marginTop: 1, gap: 3, children: [_jsxs(Text, { color: STATE_COLOR[state], children: ["\u25CF ", busy ? "…" : t(`state.${state}`)] }), _jsxs(Text, { color: MUTED, children: [dbTypeLabel(db), " \u00B7 ", formatMB(db.allocatedMemoryMB), " \u00B7 ", db.allocatedVCpu ?? "?", " vCPU"] })] }), _jsx(Box, { marginTop: 1, children: _jsx(InfoRows, { rows: [
|
|
299
|
+
[t("db.disk"), db.diskLimitBytes ? `${formatBytes(db.diskUsageBytes ?? 0)} / ${formatBytes(db.diskLimitBytes)}` : "—"],
|
|
300
|
+
[t("db.user"), db.connection?.username ?? "—"],
|
|
301
|
+
[t("db.database"), db.connection?.database ?? "—"],
|
|
302
|
+
[t("apps.info.id"), db._id],
|
|
303
|
+
] }) }), url && (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { color: MUTED, children: t("db.url") }), _jsx(Text, { color: BLUE, children: url }), _jsx(Text, { color: FAINT, children: t("db.passwordNote") })] })), _jsx(Box, { marginTop: 1, children: _jsx(PowerMenu, { state: state, busy: busy, onPower: onPower, onBack: onBack }) })] }));
|
|
304
|
+
}
|
|
305
|
+
const MAX_LINES = 2000;
|
|
306
|
+
/** The live console: follows the newest lines; ↑↓, PgUp/PgDn and the wheel scroll back; End follows again. */
|
|
307
|
+
function Console({ app, client, height, onBack }) {
|
|
308
|
+
const [lines, setLines] = useState([]);
|
|
309
|
+
const [status, setStatus] = useState(t("tui.logs.connecting"));
|
|
310
|
+
const [scroll, setScroll] = useState(0); // lines up from the bottom
|
|
311
|
+
const pending = useRef([]);
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
const abort = new AbortController();
|
|
314
|
+
// Lines are flushed to the screen a few times a second, not once per line.
|
|
315
|
+
const flush = setInterval(() => {
|
|
316
|
+
if (pending.current.length === 0)
|
|
317
|
+
return;
|
|
318
|
+
const batch = pending.current;
|
|
319
|
+
pending.current = [];
|
|
320
|
+
setLines((previous) => [...previous, ...batch].slice(-MAX_LINES));
|
|
321
|
+
}, 100);
|
|
322
|
+
const onEvent = (event) => {
|
|
323
|
+
if (event.type === "ready")
|
|
324
|
+
setStatus(event.online ? t("tui.logs.live") : t("tui.logs.offline"));
|
|
325
|
+
else if (event.type === "clear") {
|
|
326
|
+
pending.current = [];
|
|
327
|
+
setLines([]);
|
|
328
|
+
}
|
|
329
|
+
else if (event.type === "log")
|
|
330
|
+
pending.current.push(...event.text.replace(/\n$/, "").split("\n"));
|
|
331
|
+
else if (event.type === "status")
|
|
332
|
+
pending.current.push(`— ${t("logs.status", { action: event.action })}`);
|
|
333
|
+
else if (event.type === "end" && event.reason !== "timeout")
|
|
334
|
+
setStatus(event.message ? localizeServerMessage(event.message) : t("logs.ended"));
|
|
335
|
+
};
|
|
336
|
+
(async () => {
|
|
337
|
+
for (let attempt = 0; !abort.signal.aborted; attempt++) {
|
|
338
|
+
try {
|
|
339
|
+
const reason = await streamLogs(client, app._id, { tail: attempt === 0 ? 200 : 0, signal: abort.signal, onEvent });
|
|
340
|
+
if (reason !== "timeout")
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
if (!abort.signal.aborted)
|
|
345
|
+
setStatus(errorText(error));
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
})();
|
|
350
|
+
return () => { abort.abort(); clearInterval(flush); };
|
|
351
|
+
}, [client, app._id]);
|
|
352
|
+
const pageSize = Math.max(3, height - 2);
|
|
353
|
+
const maxScroll = Math.max(0, lines.length - pageSize);
|
|
354
|
+
const move = (delta) => setScroll((value) => Math.max(0, Math.min(maxScroll, value + delta)));
|
|
355
|
+
useInput((input, key) => {
|
|
356
|
+
if (key.escape || key.leftArrow || key.backspace || input === "q")
|
|
357
|
+
onBack();
|
|
358
|
+
else if (key.upArrow)
|
|
359
|
+
move(1);
|
|
360
|
+
else if (key.downArrow)
|
|
361
|
+
move(-1);
|
|
362
|
+
else if (key.pageUp)
|
|
363
|
+
move(pageSize);
|
|
364
|
+
else if (key.pageDown)
|
|
365
|
+
move(-pageSize);
|
|
366
|
+
else if (key.end || input === "G")
|
|
367
|
+
setScroll(0);
|
|
368
|
+
else if (key.home || input === "g")
|
|
369
|
+
setScroll(maxScroll);
|
|
370
|
+
});
|
|
371
|
+
useWheel((direction) => move(direction === -1 ? 3 : -3));
|
|
372
|
+
const end = lines.length - scroll;
|
|
373
|
+
const shown = useMemo(() => lines.slice(Math.max(0, end - pageSize), end), [lines, end, pageSize]);
|
|
374
|
+
return (_jsxs(Box, { flexDirection: "column", flexGrow: 1, children: [_jsxs(Box, { gap: 1, children: [_jsx(Clickable, { onClick: onBack, children: _jsxs(Text, { color: MUTED, children: ["\u2190 ", app.name] }) }), _jsx(Text, { color: FAINT, children: "/" }), _jsx(Text, { bold: true, children: t("tui.action.logs") }), _jsxs(Text, { color: FAINT, children: ["\u00B7 ", status, scroll > 0 ? ` · ${t("tui.logs.paused")}` : ""] })] }), _jsx(Box, { flexDirection: "column", marginTop: 1, flexGrow: 1, children: shown.length === 0 ? _jsx(Text, { color: FAINT, children: t("tui.logs.empty") }) : shown.map((line, position) => _jsx(Text, { wrap: "truncate-end", children: line }, position)) })] }));
|
|
375
|
+
}
|
|
376
|
+
function Account({ me, apiUrl, via, team }) {
|
|
377
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(InfoRows, { rows: [
|
|
378
|
+
[t("whoami.name"), me.name ?? "—"],
|
|
379
|
+
[t("whoami.email"), me.email],
|
|
380
|
+
[t("whoami.plan"), me.currentSubscription?.planReference?.name ?? t("whoami.noPlan")],
|
|
381
|
+
[t("whoami.via"), t(`whoami.via.${via}`)],
|
|
382
|
+
[t("doctor.team"), team ? team.name : t("team.personal")],
|
|
383
|
+
[t("whoami.api"), apiUrl],
|
|
384
|
+
] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: FAINT, children: t("tui.account.logout") }) })] }));
|
|
385
|
+
}
|
|
386
|
+
/** The application's variables, values hidden until v. Read only: changing them is `campos env set`. */
|
|
387
|
+
function EnvView({ app, client, onBack }) {
|
|
388
|
+
const [vars, setVars] = useState(undefined);
|
|
389
|
+
const [error, setError] = useState(null);
|
|
390
|
+
const [reveal, setReveal] = useState(false);
|
|
391
|
+
useEffect(() => { readEnv(client, app._id).then(setVars).catch((reason) => setError(errorText(reason))); }, [client, app._id]);
|
|
392
|
+
useInput((input, key) => {
|
|
393
|
+
if (input === "v")
|
|
394
|
+
setReveal((value) => !value);
|
|
395
|
+
else if (key.escape || key.leftArrow || key.backspace || input === "q")
|
|
396
|
+
onBack();
|
|
397
|
+
});
|
|
398
|
+
const width = Math.max(8, ...(vars ?? []).map(({ key }) => key.length));
|
|
399
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Breadcrumb, { parent: app.name, name: t("tui.env.title"), onBack: onBack }), _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [error && _jsx(Text, { color: RED, children: error }), vars === undefined && !error && _jsx(Text, { color: MUTED, children: t("tui.loading") }), vars === null && _jsx(Text, { color: MUTED, children: t("env.noPermission", { name: app.name }) }), vars && vars.length === 0 && _jsx(Text, { color: MUTED, children: t("env.none", { name: app.name }) }), vars?.map(({ key, value }) => (_jsxs(Text, { wrap: "truncate-end", children: [_jsx(Text, { children: key.padEnd(width) }), " ", _jsx(Text, { color: reveal ? "white" : FAINT, children: reveal ? value : maskValue(value) })] }, key)))] })] }));
|
|
400
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { render } from "ink-testing-library";
|
|
5
|
+
import { ApiClient } from "../core/api.js";
|
|
6
|
+
import { ClickContext, ClickRegistry } from "./clickable.js";
|
|
7
|
+
import { Dashboard } from "./App.js";
|
|
8
|
+
process.env.CAMPOS_LANG = "pt";
|
|
9
|
+
// Switching team writes config.json: keep it in a temporary folder.
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
process.env.CAMPOS_CONFIG_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "campos-tui-"));
|
|
14
|
+
const APPS = [
|
|
15
|
+
{ _id: "a1", name: "api-bot", status: "active", allocatedMemoryMB: 512, runtimeEnvironment: "nodejs", runtimeVersion: "20", currentResourceMetrics: { online: true, memoryUsageBytes: 200 * 1024 * 1024, cpuUsagePercent: 3 } },
|
|
16
|
+
{ _id: "a2", name: "loja", status: "active", allocatedMemoryMB: 256, runtimeEnvironment: "bun", currentResourceMetrics: { online: false } },
|
|
17
|
+
];
|
|
18
|
+
const TEAM_APPS = [{ _id: "a9", name: "site-do-time", status: "active", allocatedMemoryMB: 256, currentResourceMetrics: { online: true } }];
|
|
19
|
+
const DBS = [
|
|
20
|
+
{ _id: "d1", name: "principal", type: "postgresql", status: "active", allocatedMemoryMB: 512, connection: { host: "db.camposcloud.app", port: 30001, username: "app", database: "loja" }, currentResourceMetrics: { online: true } },
|
|
21
|
+
];
|
|
22
|
+
/** A control that answers the lists and records every call. */
|
|
23
|
+
function fakeControl() {
|
|
24
|
+
const calls = [];
|
|
25
|
+
const fetchImpl = async (url, init) => {
|
|
26
|
+
const path = String(url).replace("http://api", "");
|
|
27
|
+
calls.push(`${init?.method ?? "GET"} ${path}`);
|
|
28
|
+
if (path.startsWith("/v1/apps?"))
|
|
29
|
+
return Response.json({ applications: path.includes("team=t1") ? TEAM_APPS : APPS });
|
|
30
|
+
if (path.startsWith("/v1/databases?"))
|
|
31
|
+
return Response.json({ databases: path.includes("team=t1") ? [] : DBS });
|
|
32
|
+
if (path === "/v1/teams")
|
|
33
|
+
return Response.json([{ _id: "t1", name: "Estúdio", owner: "u", members: [] }]);
|
|
34
|
+
if (/^\/v1\/apps\/a\d$/.test(path))
|
|
35
|
+
return Response.json({ ...APPS.find((app) => path.endsWith(app._id)), environmentVariables: [{ key: "TOKEN", value: "segredo-123" }] });
|
|
36
|
+
return Response.json({ ok: true });
|
|
37
|
+
};
|
|
38
|
+
return { client: new ApiClient("http://api", "token", fetchImpl), calls };
|
|
39
|
+
}
|
|
40
|
+
const tick = (ms = 80) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
41
|
+
const KEYS = { down: "\x1b[B", up: "\x1b[A", right: "\x1b[C", left: "\x1b[D", enter: "\r", escape: "\x1b" };
|
|
42
|
+
function mount() {
|
|
43
|
+
const control = fakeControl();
|
|
44
|
+
const registry = new ClickRegistry();
|
|
45
|
+
const view = render(_jsx(ClickContext.Provider, { value: registry, children: _jsx(Dashboard, { client: control.client, me: { _id: "u", email: "marina@exemplo.com", name: "Marina" }, apiUrl: "http://api", via: "cli", team: null }) }));
|
|
46
|
+
const frame = () => view.lastFrame() ?? "";
|
|
47
|
+
const press = async (...keys) => { for (const key of keys) {
|
|
48
|
+
view.stdin.write(key);
|
|
49
|
+
await tick();
|
|
50
|
+
} };
|
|
51
|
+
return { ...view, ...control, registry, frame, press };
|
|
52
|
+
}
|
|
53
|
+
test("só com as setas: escolher, abrir, o menu de ações e voltar", async () => {
|
|
54
|
+
const view = mount();
|
|
55
|
+
await tick();
|
|
56
|
+
assert.match(view.frame(), /2 aplicações · 1 online/);
|
|
57
|
+
assert.match(view.frame(), /▍ api-bot/);
|
|
58
|
+
await view.press(KEYS.down);
|
|
59
|
+
assert.match(view.frame(), /▍ loja/);
|
|
60
|
+
await view.press(KEYS.enter);
|
|
61
|
+
assert.match(view.frame(), /← Aplicações \/ loja/);
|
|
62
|
+
// Stopped: the first action is to start it, and there is nothing to stop.
|
|
63
|
+
assert.match(view.frame(), /› Ligar/);
|
|
64
|
+
assert.doesNotMatch(view.frame(), /Desligar/);
|
|
65
|
+
await view.press(KEYS.escape);
|
|
66
|
+
assert.match(view.frame(), /▍ loja/);
|
|
67
|
+
view.unmount();
|
|
68
|
+
});
|
|
69
|
+
test("Enter no menu executa; desligar pergunta antes, também com setas", async () => {
|
|
70
|
+
const view = mount();
|
|
71
|
+
await tick();
|
|
72
|
+
await view.press(KEYS.enter);
|
|
73
|
+
assert.match(view.frame(), /› Reiniciar/);
|
|
74
|
+
await view.press(KEYS.enter);
|
|
75
|
+
await tick(80);
|
|
76
|
+
assert.ok(view.calls.includes("POST /v1/apps/a1/restart"), view.calls.join("\n"));
|
|
77
|
+
assert.match(view.frame(), /api-bot reiniciada/);
|
|
78
|
+
// Reiniciar, Console, Variáveis de ambiente, Desligar, Voltar: three down is Desligar.
|
|
79
|
+
await view.press(KEYS.down, KEYS.down, KEYS.down);
|
|
80
|
+
assert.match(view.frame(), /› Desligar/);
|
|
81
|
+
await view.press(KEYS.enter);
|
|
82
|
+
assert.match(view.frame(), /Desligar agora\?/);
|
|
83
|
+
assert.match(view.frame(), /› Sim, desligar/);
|
|
84
|
+
assert.ok(!view.calls.includes("POST /v1/apps/a1/stop"));
|
|
85
|
+
await view.press(KEYS.down, KEYS.enter); // Cancelar
|
|
86
|
+
assert.doesNotMatch(view.frame(), /Desligar agora\?/);
|
|
87
|
+
assert.ok(!view.calls.includes("POST /v1/apps/a1/stop"));
|
|
88
|
+
await view.press(KEYS.down, KEYS.down, KEYS.down, KEYS.enter, KEYS.enter); // Desligar → Sim, desligar
|
|
89
|
+
await tick(80);
|
|
90
|
+
assert.ok(view.calls.includes("POST /v1/apps/a1/stop"));
|
|
91
|
+
view.unmount();
|
|
92
|
+
});
|
|
93
|
+
test("reiniciar uma aplicação desligada liga ela", async () => {
|
|
94
|
+
const view = mount();
|
|
95
|
+
await tick();
|
|
96
|
+
await view.press(KEYS.down, "r");
|
|
97
|
+
await tick(80);
|
|
98
|
+
assert.ok(view.calls.includes("POST /v1/apps/a2/start"), view.calls.join("\n"));
|
|
99
|
+
assert.ok(!view.calls.includes("POST /v1/apps/a2/restart"));
|
|
100
|
+
assert.match(view.frame(), /loja ligada/);
|
|
101
|
+
view.unmount();
|
|
102
|
+
});
|
|
103
|
+
test("→ troca de aba: os bancos com a conexão, e a conta", async () => {
|
|
104
|
+
const view = mount();
|
|
105
|
+
await tick();
|
|
106
|
+
await view.press(KEYS.right);
|
|
107
|
+
assert.match(view.frame(), /1 bancos · 1 online/);
|
|
108
|
+
assert.match(view.frame(), /▍ principal\s+PostgreSQL/);
|
|
109
|
+
await view.press(KEYS.enter);
|
|
110
|
+
assert.match(view.frame(), /postgresql:\/\/app:<password>@db\.camposcloud\.app:30001\/loja/);
|
|
111
|
+
assert.match(view.frame(), /› Reiniciar/);
|
|
112
|
+
await view.press(KEYS.enter);
|
|
113
|
+
await tick(80);
|
|
114
|
+
assert.ok(view.calls.includes("POST /v1/databases/d1/restart"));
|
|
115
|
+
await view.press(KEYS.escape, KEYS.right);
|
|
116
|
+
assert.match(view.frame(), /marina@exemplo\.com/);
|
|
117
|
+
assert.match(view.frame(), /login da CLI/);
|
|
118
|
+
await view.press(KEYS.right);
|
|
119
|
+
assert.match(view.frame(), /▍ api-bot/);
|
|
120
|
+
view.unmount();
|
|
121
|
+
});
|
|
122
|
+
test("o mouse: clicar na linha escolhe, de novo abre, no menu executa, na aba troca", async () => {
|
|
123
|
+
const view = mount();
|
|
124
|
+
await tick();
|
|
125
|
+
const lojaRow = view.frame().split("\n").findIndex((line) => line.includes("loja"));
|
|
126
|
+
view.registry.dispatch({ button: 0, x: 6, y: lojaRow, kind: "release" });
|
|
127
|
+
await tick();
|
|
128
|
+
assert.match(view.frame(), /▍ loja/);
|
|
129
|
+
view.registry.dispatch({ button: 0, x: 6, y: lojaRow, kind: "release" });
|
|
130
|
+
await tick();
|
|
131
|
+
assert.match(view.frame(), /← Aplicações \/ loja/);
|
|
132
|
+
const startRow = view.frame().split("\n").findIndex((line) => line.includes("Ligar"));
|
|
133
|
+
view.registry.dispatch({ button: 0, x: 4, y: startRow, kind: "release" });
|
|
134
|
+
await tick(120);
|
|
135
|
+
assert.ok(view.calls.includes("POST /v1/apps/a2/start"));
|
|
136
|
+
const header = view.frame().split("\n")[0];
|
|
137
|
+
view.registry.dispatch({ button: 0, x: header.indexOf("Bancos") + 1, y: 0, kind: "release" });
|
|
138
|
+
await tick();
|
|
139
|
+
assert.match(view.frame(), /principal/);
|
|
140
|
+
view.unmount();
|
|
141
|
+
});
|
|
142
|
+
test("a rodinha do mouse move a escolha na lista", async () => {
|
|
143
|
+
const view = mount();
|
|
144
|
+
await tick();
|
|
145
|
+
view.registry.dispatch({ button: 65, x: 0, y: 0, kind: "wheel" });
|
|
146
|
+
await tick();
|
|
147
|
+
assert.match(view.frame(), /▍ loja/);
|
|
148
|
+
view.registry.dispatch({ button: 64, x: 0, y: 0, kind: "wheel" });
|
|
149
|
+
await tick();
|
|
150
|
+
assert.match(view.frame(), /▍ api-bot/);
|
|
151
|
+
view.unmount();
|
|
152
|
+
});
|
|
153
|
+
test("t troca de time: a lista passa a ser a do time e o cabeçalho mostra o nome", async () => {
|
|
154
|
+
const view = mount();
|
|
155
|
+
await tick();
|
|
156
|
+
assert.match(view.frame(), /Time: pessoal/);
|
|
157
|
+
await view.press("t");
|
|
158
|
+
await tick(80);
|
|
159
|
+
assert.match(view.frame(), /Trocar de time/);
|
|
160
|
+
assert.match(view.frame(), /› Conta pessoal\s+· ativo/);
|
|
161
|
+
await view.press(KEYS.down, KEYS.enter);
|
|
162
|
+
await tick(120);
|
|
163
|
+
assert.match(view.frame(), /Time: Estúdio/);
|
|
164
|
+
assert.match(view.frame(), /site-do-time/);
|
|
165
|
+
assert.ok(view.calls.some((call) => call.startsWith("GET /v1/apps?") && call.includes("team=t1")));
|
|
166
|
+
view.unmount();
|
|
167
|
+
});
|
|
168
|
+
test("variáveis de ambiente: escondidas, v mostra", async () => {
|
|
169
|
+
const view = mount();
|
|
170
|
+
await tick();
|
|
171
|
+
await view.press(KEYS.enter, KEYS.down, KEYS.down, KEYS.enter); // api-bot → Variáveis de ambiente
|
|
172
|
+
await tick(80);
|
|
173
|
+
assert.match(view.frame(), /TOKEN\s+seg•+/);
|
|
174
|
+
assert.doesNotMatch(view.frame(), /segredo-123/);
|
|
175
|
+
await view.press("v");
|
|
176
|
+
assert.match(view.frame(), /segredo-123/);
|
|
177
|
+
view.unmount();
|
|
178
|
+
});
|
|
179
|
+
test("campos <app> abre o painel direto na aplicação", async () => {
|
|
180
|
+
const control = fakeControl();
|
|
181
|
+
const view = render(_jsx(ClickContext.Provider, { value: new ClickRegistry(), children: _jsx(Dashboard, { client: control.client, me: { _id: "u", email: "marina@exemplo.com" }, apiUrl: "http://api", via: "cli", team: null, initialAppId: "a2" }) }));
|
|
182
|
+
await tick(120);
|
|
183
|
+
assert.match(view.lastFrame() ?? "", /← Aplicações \/ loja/);
|
|
184
|
+
assert.match(view.lastFrame() ?? "", /› Ligar/);
|
|
185
|
+
view.stdin.write(KEYS.escape);
|
|
186
|
+
await tick();
|
|
187
|
+
assert.match(view.lastFrame() ?? "", /▍ api-bot|loja/);
|
|
188
|
+
view.unmount();
|
|
189
|
+
});
|