Skip to content
8 changes: 1 addition & 7 deletions src/commands/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { ApiClient, requireProject } from '../api.js'
import { info, printJson } from '../util.js'

function qs(params: Record<string, string | undefined>): string {
const u = new URLSearchParams()
for (const [k, v] of Object.entries(params)) if (v !== undefined && v !== '') u.set(k, String(v))
const s = u.toString()
return s ? `?${s}` : ''
}
import { qs } from './services.js'

// One printed series line. Pure seam so the formatting is testable without a backend.
//
Expand Down
15 changes: 14 additions & 1 deletion src/commands/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ export const SERVICE_TYPES = ['postgres', 'storage', 'compute', 'redis', 'mysql'
export type ServiceType = (typeof SERVICE_TYPES)[number]
const SERVICE_NAME_RE = /^[a-z0-9][a-z0-9-]{0,38}$/

// ---- pure, unit-tested helpers (throw plain Errors; the CLI guard turns them into clean output) ----

export function q(branch?: string): string {
return branch ? `?branch=${encodeURIComponent(branch)}` : ''
}

// ---- pure, unit-tested helpers (throw plain Errors; the CLI guard turns them into clean output) ----
// Multi-param form of `q`, shared by storage and metrics. Skips undefined and '' and returns ''
// rather than a bare '?' when nothing is left. That means a caller can never send an
// intentionally-empty param (e.g. `?prefix=`); if you need one, don't reach for this helper.
// Encodes via URLSearchParams (form-urlencoded), which escapes more than `q`'s encodeURIComponent
// does: a space becomes '+' rather than '%20', and `~ ' ( ) !` are percent-encoded where `q` leaves
// them bare. Swapping one helper for the other changes the wire format for such values.
export function qs(params: Record<string, string | undefined>): string {
const u = new URLSearchParams()
for (const [k, v] of Object.entries(params)) if (v !== undefined && v !== '') u.set(k, v)
const s = u.toString()
return s ? `?${s}` : ''
}

// Validate a service-type argument against the allowed set for a command.
export function assertType(type: string, allowed: readonly string[] = SERVICE_TYPES): asserts type is ServiceType {
Expand Down
9 changes: 1 addition & 8 deletions src/commands/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import { Readable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { ApiClient, requireProject } from '../api.js'
import { info, printJson, handleApproval } from '../util.js'
import { q, resolveSoleService, parseAccess } from './services.js'
import { q, qs, resolveSoleService, parseAccess } from './services.js'
import { fmtBytes } from './postgres.js' // the repo's tested bytes formatter — don't grow a third copy

type Common = { branch?: string; service?: string; json?: boolean }

function qs(params: Record<string, string | undefined>): string {
const u = new URLSearchParams()
for (const [k, v] of Object.entries(params)) if (v !== undefined && v !== '') u.set(k, String(v))
const s = u.toString()
return s ? `?${s}` : ''
}

// Page size the listing route accepts. Junk must fail here, not travel as `limit=NaN`.
export function parseObjectLimit(raw: string): number {
const n = Number(raw)
Expand Down
35 changes: 34 additions & 1 deletion test/services.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
import { describe, it, expect } from 'vitest'
import {
assertType, assertServiceName, parseCount, parsePort, parseAccess, resolveServiceId, resolveComputeServiceId, SERVICE_TYPES,
servicesAddRequestBody, servicesAdd, serviceListLine, serviceAddedLine, parsePgVersion,
servicesAddRequestBody, servicesAdd, serviceListLine, serviceAddedLine, parsePgVersion, q, qs,
} from '../src/commands/services.js'

// `qs` is shared by storage and metrics, so its contract is pinned here rather than only through
// the path builders of those modules.
describe('qs', () => {
it('returns an empty string, not a bare "?", when every param is absent', () => {
expect(qs({})).toBe('')
expect(qs({ branch: undefined, group: '' })).toBe('')
})
it('skips undefined and empty-string values, keeping the rest in insertion order', () => {
expect(qs({ branch: 'main', group: undefined, prefix: '', limit: '50' })).toBe('?branch=main&limit=50')
})
// Only undefined and '' count as absent. '0' is a real value: metrics' `--from` can resolve to epoch 0
// and arrives here stringified. (The string '0' is truthy, so this does not distinguish the current
// guard from `if (v)`; it pins the contract for whoever widens the signature to numbers later.)
it('keeps a "0" value rather than treating it as absent', () => {
expect(qs({ limit: '0' })).toBe('?limit=0')
expect(qs({ from: '0', to: '0' })).toBe('?from=0&to=0')
})
it('encodes values the URLSearchParams way (space becomes "+", "+" and "&" are escaped)', () => {
expect(qs({ prefix: 'a b+c&d' })).toBe('?prefix=a+b%2Bc%26d')
})
// q() uses encodeURIComponent, which differs from URLSearchParams on a space ('%20' vs '+') and on
// the characters it leaves bare (! ' ( ) ~). For everything else the two helpers agree.
it('agrees with q() for an alphanumeric branch path', () => {
expect(qs({ branch: 'feat/x' })).toBe(q('feat/x'))
expect(qs({ branch: undefined })).toBe(q(undefined))
})
it('diverges from q() on a space and on the punctuation encodeURIComponent leaves bare', () => {
const v = "a~b'c(d)e!f*g h"
expect(qs({ branch: v })).toBe('?branch=a%7Eb%27c%28d%29e%21f*g+h')
expect(q(v)).toBe("?branch=a~b'c(d)e!f*g%20h")
})
})

describe('assertType', () => {
it('accepts valid service types', () => {
for (const t of SERVICE_TYPES) expect(() => assertType(t)).not.toThrow()
Expand Down
Loading