forked from TestSprite/testsprite-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.ts
More file actions
231 lines (207 loc) · 8.19 KB
/
Copy pathusage.ts
File metadata and controls
231 lines (207 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* `testsprite usage` — show the account's credit balance and plan/entitlement
* info as a proactive pre-flight before a large `test run` fan-out.
*
* Backend note: the `GET /me` endpoint does NOT currently return credit balance
* or plan info. This command calls `/me` for auth-identity fields and surfaces
* the `credits` / `subPlan` fields when and only when the backend supplies them
* (forward-compat / absent-safe). A dedicated backend endpoint is a required
* follow-up.
*
* BACKEND FOLLOW-UP REQUIRED:
* Add `credits`, `subPlan` to the `/me` response, or add a dedicated
* `GET /api/cli/v1/usage` endpoint returning `{ credits, subPlan, creditsPerRun }`.
*/
import { Command } from 'commander';
import {
emitDryRunBanner,
makeHttpClient,
parseRequestTimeoutFlag,
type CommonOptions as FactoryCommonOptions,
} from '../lib/client-factory.js';
import { loadConfig } from '../lib/config.js';
import { resolvePortalBase } from '../lib/facade.js';
import type { FetchImpl } from '../lib/http.js';
import { GLOBAL_OPTS_HINT, Output, type OutputMode } from '../lib/output.js';
/**
* Usage/balance response from `/me` (when the backend supplies it) or a future
* `/usage` endpoint.
*
* All fields except `userId`/`keyId`/`env` are forward-compat: the backend
* does not return them today. They are rendered only when present.
*/
export interface UsageResponse {
userId: string;
keyId: string;
env: 'development' | 'staging' | 'production';
/**
* Remaining credit balance. Present only when the backend /me (or /usage)
* includes the User.credits projection. BACKEND FOLLOW-UP: me.controller.ts.
*/
credits?: number;
/**
* Subscription plan name (e.g. "Free", "Standard", "Pro"). Present only when
* the backend /me (or /usage) includes the User.subPlan projection.
* BACKEND FOLLOW-UP: me.controller.ts.
*/
subPlan?: string;
/**
* Credit cost per test run trigger (informational). Present only when the
* backend supplies it.
*/
creditsPerRun?: number;
}
export interface UsageDeps {
env?: NodeJS.ProcessEnv;
credentialsPath?: string;
fetchImpl?: FetchImpl;
stdout?: (line: string) => void;
stderr?: (line: string) => void;
}
type CommonOptions = FactoryCommonOptions;
/** Dry-run canned response — matches what the real /me + User lookup would return. */
export const DRY_RUN_USAGE_SAMPLE: UsageResponse = {
userId: '11111111-1111-4111-8111-111111111111',
keyId: 'key_dryrun_2026',
env: 'development',
credits: 42,
subPlan: 'Standard',
creditsPerRun: 2,
};
/**
* Run the `usage` command. Calls `GET /me`, surfaces identity + any
* credits/plan fields the backend supplies. Absent fields are silently
* omitted (forward-compat until the backend adds the projection).
*/
export async function runUsage(opts: CommonOptions, deps: UsageDeps = {}): Promise<UsageResponse> {
const stderr = deps.stderr ?? ((line: string) => process.stderr.write(`${line}\n`));
const out = makeOutput(opts.output, deps);
if (opts.dryRun) {
emitDryRunBanner(stderr);
stderr('[note] credit balance requires a backend update — showing dry-run sample values');
out.print(DRY_RUN_USAGE_SAMPLE, data => renderUsage(data as UsageResponse));
return DRY_RUN_USAGE_SAMPLE;
}
const client = makeHttpClient(opts, {
env: deps.env,
credentialsPath: deps.credentialsPath,
fetchImpl: deps.fetchImpl,
stderr: deps.stderr,
});
// Environment-correct portal origin for billing/upgrade links (dev and prod
// portals live on different domains — never hardcode). Resolved from the
// same credentials the client was just built from; undefined for unknown
// hosts (links then render as routes only).
const portalBase = resolvePortalBase(
loadConfig({
profile: opts.profile,
endpointUrl: opts.endpointUrl,
env: deps.env,
credentialsPath: deps.credentialsPath,
}).apiUrl,
);
const billingUrl =
portalBase !== undefined
? `${portalBase}/dashboard/settings/billing`
: 'the portal Billing page (/dashboard/settings/billing)';
// /me is the only available source of credits/plan today.
// When the backend adds credits/subPlan to MeResponse (or adds /usage),
// this single get call is sufficient — no code change needed in the CLI.
const me = await client.get<UsageResponse>('/me');
out.print(me, data => renderUsage(data as UsageResponse, portalBase));
// In text mode, emit a backend-gap note when credits are missing so the
// user knows why the balance isn't shown (instead of assuming zero or error).
if (opts.output === 'text' && me.credits === undefined) {
stderr(
'[note] credit balance not available — backend does not yet expose credits on /me.' +
` Check ${billingUrl} for your current balance.`,
);
}
return me;
}
function renderUsage(u: UsageResponse, portalBase?: string): string {
const lines: string[] = [];
// Identity block (always present)
lines.push(`userId: ${u.userId}`);
lines.push(`keyId: ${u.keyId}`);
lines.push(`env: ${u.env}`);
// Balance block — shown only when the backend supplies it
const hasBalanceData = u.credits !== undefined || u.subPlan !== undefined;
if (hasBalanceData) {
lines.push('');
lines.push('--- credits & plan ---');
if (u.subPlan !== undefined) {
lines.push(`plan: ${u.subPlan}`);
}
if (u.credits !== undefined) {
lines.push(`credits: ${u.credits}`);
}
if (u.creditsPerRun !== undefined) {
lines.push(`cost per frontend run: ${u.creditsPerRun} credit(s)`);
lines.push(
`cost per backend run: 0 credit(s) (backend tests bill at code-generation, not at run time)`,
);
}
// Pre-flight hint: how many runs the current balance can fund
if (u.credits !== undefined && u.creditsPerRun !== undefined && u.creditsPerRun > 0) {
const maxRuns = Math.floor(u.credits / u.creditsPerRun);
lines.push(`can trigger: ~${maxRuns} run(s) at current balance`);
}
}
// Actionable upgrade line for Free or low-balance keys
const isLowBalance =
u.credits !== undefined && u.creditsPerRun !== undefined && u.credits < u.creditsPerRun;
const isFree = u.subPlan?.toLowerCase() === 'free';
if (isLowBalance) {
lines.push('');
lines.push(
'warning: credit balance is below the per-run cost. Top up at:' +
` ${portalBase !== undefined ? `${portalBase}/dashboard/settings/billing` : 'the portal Billing page (/dashboard/settings/billing)'}`,
);
} else if (isFree) {
lines.push('');
lines.push(
'note: on Free plan — upgrade for more credits and higher run limits:' +
` ${portalBase !== undefined ? `${portalBase}/pricing` : 'the portal Pricing page (/pricing)'}`,
);
}
return lines.join('\n');
}
export function createUsageCommand(deps: UsageDeps = {}): Command {
const cmd = new Command('usage')
.alias('credits')
.description(
'Show credit balance and plan/entitlement info (proactive pre-flight before a large test run)',
)
.addHelpText('after', GLOBAL_OPTS_HINT)
.addHelpText(
'after',
'\nExamples:\n' +
' testsprite usage # show balance + plan\n' +
' testsprite usage --output json # machine-readable balance\n' +
' testsprite credits # alias for usage\n' +
'\nNote: credit balance requires a backend update to /me. Until shipped,\n' +
" check your portal's Billing page (/dashboard/settings/billing) for your balance.",
)
.action(async (_cmdOpts, command: Command) => {
await runUsage(resolveCommonOptions(command), deps);
});
return cmd;
}
function resolveCommonOptions(command: Command): CommonOptions {
const globals = command.optsWithGlobals() as Partial<CommonOptions> & {
requestTimeout?: string;
};
return {
profile: globals.profile ?? 'default',
output: globals.output ?? 'text',
endpointUrl: globals.endpointUrl,
debug: globals.debug ?? false,
verbose: globals.verbose ?? false,
dryRun: globals.dryRun ?? false,
requestTimeoutMs: parseRequestTimeoutFlag(globals.requestTimeout),
};
}
function makeOutput(mode: OutputMode, deps: UsageDeps): Output {
return new Output(mode, { stdout: deps.stdout, stderr: deps.stderr });
}