Skip to content
21 changes: 21 additions & 0 deletions plugins/altimate-code/hooks/altimate-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ test('a write call never asks the MCP server, even when one is connected', async
expect(argvs.map(argv => argv.slice(1))[1]).toEqual(['tool', 'run', 'dbt_manifest', '--args', '{}', '--json'])
})

test('a failure with no answer object keeps its first line of text; one with an object keeps the object\'s', async () => {
const failing = (run: () => unknown) => {
const { $ } = standIn(() => null)
$.process.run = async () => run()
return $
}
const text = async (run: () => unknown) => (await runTool(failing(run), 'dbt_manifest', {})).text
expect(await text(() => ({ exitCode: 1, stdout: 'Error: no such tool\nusage: altimate tool run', stderr: 'ignored' }))).toBe('Error: no such tool')
expect(await text(() => ({ exitCode: 1, stdout: '', stderr: '\nTypeError: crashed\n at main' }))).toBe('TypeError: crashed')
expect(await text(() => ({ exitCode: 1, stdout: 'x'.repeat(900), stderr: '' }))).toBe('x'.repeat(500))
expect(await text(() => ({ exitCode: 1, stdout: '', stderr: '' }))).toBe('')
// An answer cut off mid-JSON is no message: only stderr, if any, is shown.
expect(await text(() => ({ exitCode: 1, stdout: '{"status":"ok","out', stderr: '' }))).toBe('')
expect(await text(() => ({ exitCode: 1, stdout: '{"status":"ok","out', stderr: 'Killed: out of memory' }))).toBe('Killed: out of memory')
// An answer object speaks for itself: its output, or nothing, never a line of stderr.
expect(await text(() => ({ exitCode: 1, stdout: JSON.stringify({ status: 'error', output: 'boom', metadata: {} }), stderr: 'noise' }))).toBe('boom')
expect(await text(() => ({ exitCode: 1, stdout: JSON.stringify({ status: 'error', metadata: {} }), stderr: 'noise' }))).toBe('')
const result = await runTool(failing(() => ({ exitCode: 2, stdout: 'plain failure' })), 'dbt_manifest', {})
expect(result).toEqual({ metadata: null, text: 'plain failure', ok: false })
})

test('within handles the work\'s rejection before it sets the deadline, so a throwing clock leaves none unhandled', async () => {
let handled = false
// A work that rejects, recording when a rejection handler is attached.
Expand Down
6 changes: 5 additions & 1 deletion plugins/altimate-code/hooks/altimate-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,9 @@ export async function runTool($: EngineInterface, name: string, args: Record<str
if (result.exitCode === 0 && (!data || typeof data !== 'object' || typeof data.status !== 'string')) {
return { metadata: null as any, text: 'the answer could not be read', ok: false }
}
return { metadata: (data?.metadata ?? null) as any, text: data?.output ?? '', ok: result.exitCode === 0 && data?.status !== 'error' }
// A crash, or a failure the CLI prints as plain text, has no answer object: its first line is what to show.
// An answer cut off mid-JSON is no message, so only what the CLI printed to stderr is shown for it.
const out = result.stdout.trim()
const said = data && typeof data === 'object' ? '' : ((out.startsWith('{') ? '' : out) || String(result.stderr ?? '').trim()).split('\n')[0]!.slice(0, 500)
return { metadata: (data?.metadata ?? null) as any, text: data?.output ?? said, ok: result.exitCode === 0 && data?.status !== 'error' }
}
45 changes: 40 additions & 5 deletions plugins/altimate-code/hooks/features/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from 'claude-code/testing'

import { nothingToReport } from '../inert-features'
import {
attempt,
featureBandLines,
Expand All @@ -16,12 +17,22 @@ import {

const engine = () => {
const logs: string[] = []
const atoms = new Map<string, unknown>()
// Enough of the engine for a feature that keeps state, reads files and registers commands; a stub needs none of it.
const $ = {
ui: { log: (text: string) => void logs.push(text) },
fs: { exists: async () => false, read: async () => '' },
state: { get: async () => ({ value: null }), set: async () => ({ isSet: true }), update: async () => ({ isSet: true }) },
ui: { log: (text: string) => void logs.push(text), status: async () => {}, toast: () => {}, open: async () => {} },
fs: { exists: async () => false, read: async () => '', list: async () => [], stat: async () => null },
state: {
get: async (key: unknown) => ({ value: atoms.get(JSON.stringify(key)) }),
set: async (key: unknown, value: unknown) => (atoms.set(JSON.stringify(key), value), { isSet: true }),
},
command: { register: async () => {} },
// What the features that are built ask of the session: a project with nothing to report, no CLI beyond the data tools, no clock work run.
process: { run: async (argv: readonly string[]) => nothingToReport(argv) ?? { exitCode: 1, stdout: '', stderr: 'no CLI here' } },
clock: { now: async () => 0, after: () => {}, every: () => {}, sleep: async () => {} },
session: { root: async () => '/repo', cwd: async () => '/repo' },
store: { get: async () => null, set: async () => {} },
mcp: { connect: async () => ({ isConnected: false, reason: 'not_connected', message: 'off' }) },
}
return { logs, $: $ as any }
}
Expand All @@ -40,7 +51,30 @@ test('attempt logs a feature function that throws at once, one that rejects, and
expect(logs).toEqual(['runs · sessionStart failed: Error: at once', 'defer · bandLine failed: Error: later'])
})

test('with every feature a stub, each phase runs through the features and logs nothing', async () => {
test('no feature fails in any phase: every failure would be logged, and none is', async () => {
const { $, logs } = engine()
// The engine does record a failure: a feature function that throws is logged through it.
await attempt($, 'runs', 'sessionStart', () => {
throw new Error('broken')
})
expect(logs.splice(0)).toEqual(['runs · sessionStart failed: Error: broken'])
const bash = { tool: 'Bash', command: 'dbt run -s a' } as any
const file = { tool: 'Edit', file_path: '/repo/models/a.sql' } as any
await featuresSessionStart($, {} as any)
await featuresTurnStart($, {} as any)
await featureBandLines($, {} as any)
await featuresBashRewrite($, bash)
await featuresBashGate($, bash)
await featuresBashAfter($, bash, {} as any)
await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null })
await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' })
await featuresTurnComplete($, {} as any, {} as any)
await featuresSessionEnd($, {} as any)
// A feature may log what it does; what must not appear is a failure, which is logged as `<id> · <phase> failed: …`.
expect(logs.filter(line => line.includes(' failed: '))).toEqual([])
})

test('each phase runs through the features, which find nothing to report and log nothing', async () => {
const { $, logs } = engine()
const bash = { tool: 'Bash', command: 'dbt run -s a' } as any
const file = { tool: 'Edit', file_path: '/repo/models/a.sql' } as any
Expand All @@ -51,7 +85,8 @@ test('with every feature a stub, each phase runs through the features and logs n
expect(await featureBandLines($, {} as any)).toEqual([])
expect(await featuresBashRewrite($, bash)).toBe('dbt run -s a')
expect(await featuresBashGate($, bash)).toBeNull()
expect(await featuresBashAfter($, bash, {} as any)).toEqual([])
// A dbt command may get a note from a feature that watches dbt (what a run would scan); a call no feature watches gets none.
expect(await featuresBashAfter($, { tool: 'Bash', command: 'ls models' } as any, {} as any)).toEqual([])
expect(await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null })).toEqual([])
expect(await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' })).toEqual([])
expect(logs).toEqual([])
Expand Down
3 changes: 3 additions & 0 deletions plugins/altimate-code/hooks/features/sql-graph.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, mock, test } from 'claude-code/testing'

import { nothingToReport } from '../inert-features'
import { cteCount, inputsOf, orderNodes } from './sql-graph'

const ROOT = '/repo'
Expand Down Expand Up @@ -52,6 +53,8 @@ async function start($: any, on: any, options: Options = {}) {
on('tool.list', () => ({ value: [] }))
on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } }))
on('process.run', async (_: unknown, e: { argv: readonly string[] }) => {
const inert = nothingToReport(e.argv)
if (inert) return { value: inert }
const [, command] = e.argv
// The integrations half of the plugin asks the CLI for its status at session start.
if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } }
Expand Down
20 changes: 20 additions & 0 deletions plugins/altimate-code/hooks/inert-features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// For tests: what each data tool a feature asks for answers when there is nothing to report. A test's
// fake CLI answers these first, so the calls it records are its own and a feature that is built finds a
// project with nothing to say, whichever features are built.
const NOTHING_TO_REPORT: Record<string, Record<string, unknown>> = {
docs_share_comments: { threads: [] },
dbt_health: { success: true, insights: [] },
dbt_defer_state: { success: true, enabled: false, mode: 'none' },
dbt_cost_estimate: { supported: false, adapter: 'none' },
dbt_run_results: { found: false },
// The docs feature shows a coverage line for any successful answer and passes over one that isn't, silently.
dbt_docs_coverage: { success: false },
Comment thread
hkaltai marked this conversation as resolved.
Resolved
}

/** The answer of the `altimate tool run <name>` in `argv` when `name` is one a feature asks for; null for any other command. */
export function nothingToReport(argv: readonly string[]): { exitCode: number; stdout: string; stderr: string } | null {
const [, command, sub, name] = argv
const metadata = command === 'tool' && sub === 'run' && name !== undefined ? NOTHING_TO_REPORT[name] : undefined
if (!metadata) return null
return { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata }), stderr: '' }
}
18 changes: 18 additions & 0 deletions plugins/altimate-code/hooks/lineage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, mock, test } from 'claude-code/testing'

import { commandDir } from './lineage'
import { buildGraph, subgraph } from './lineage-graph'
import { MANIFEST } from './lineage-graph.test'
import { nothingToReport } from './inert-features'

const ROOT = '/repo'
const MANIFEST_PATH = `${ROOT}/target/manifest.json`
Expand Down Expand Up @@ -189,6 +191,8 @@ async function start($: any, on: any, options: Options = {}) {
return { value: { content: [], isError: true } }
})
on('process.run', async (_: unknown, e: { argv: readonly string[] }) => {
const inert = nothingToReport(e.argv)
if (inert) return { value: inert }
const [, command, , tool] = e.argv
// A slow CLI dbt_lineage: it answers once the test lets it.
if (command === 'tool' && tool === 'dbt_lineage' && options.holdCliLineage) await options.holdCliLineage
Expand Down Expand Up @@ -1741,3 +1745,17 @@ test("the pane's MCP column trace that never answers is given up on, and the CLI
expect(calls.indexOf('mcp:impact_analysis:revenue')).toBeLessThan(calls.indexOf('cli:impact_analysis:revenue'))
expect(logs.some(line => line.includes('impact_analysis(fct_orders, column: revenue) · read by 2 models'))).toBe(true)
})

test('each cd in a command moves the folder it reads from, after any separator or blank line', () => {
expect(commandDir('cd a && cd "b c"; cd /abs\n cd rel', '/repo')).toBe('/abs/rel')
expect(commandDir('ls;\n\n cd models | cat', '/repo')).toBe('/repo/models')
expect(commandDir('echo cd nowhere', '/repo')).toBe('/repo')
})

test('a very long run of blank lines before a command is read at once', () => {
const began = Date.now()
expect(commandDir(`${'\n'.repeat(64000)}cd models`, '/repo')).toBe('/repo/models')
expect(commandDir(`${'\n'.repeat(64000)}dbt run -s orders`, '/repo')).toBe('/repo')
for (const blank of ['\r', '\r\n', '\u2028', '\u2029']) expect(commandDir(`${blank.repeat(32000)}cd models`, '/repo')).toBe('/repo/models')
expect(Date.now() - began).toBeLessThan(500)
})
7 changes: 4 additions & 3 deletions plugins/altimate-code/hooks/lineage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ function sqlFilesIn(command: string, cwd: string): string[] {
})
}

// Where a shell command reads from: the session folder, moved by each `cd` in the command.
const CD = /(?:^|[;&|(\n]\s*)cd\s+("[^"]+"|'[^']+'|[^\s;&|)]+)/gm
function commandDir(command: string, cwd: string): string {
// Where a shell command reads from: the session folder, moved by each `cd` in the command. Blanks after a
// separator stop at any line break, which starts a line itself, so a long run of blank lines is read once, not once per line.
const CD = /(?:^|[;&|(\n\r\u2028\u2029][^\S\n\r\u2028\u2029]*)cd\s+("[^"]+"|'[^']+'|[^\s;&|)]+)/gm
export function commandDir(command: string, cwd: string): string {
let dir = cwd
for (const match of command.matchAll(CD)) {
const target = match[1]!.replace(/^['"]|['"]$/g, '')
Expand Down
3 changes: 3 additions & 0 deletions plugins/altimate-code/hooks/task-impact.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, mock, test } from 'claude-code/testing'

import { MIN_ALTIMATE_CODE } from './altimate-cli'
import { DOWN, UP, embedded } from './impact-report.test'
import { nothingToReport } from './inert-features'

const ROOT = '/repo'
const MANIFEST = `${ROOT}/target/manifest.json`
Expand Down Expand Up @@ -140,6 +141,8 @@ async function start($: any, on: any, options: Options = {}) {
on('session.turns', () => ({ value: options.turns?.() ?? 1 }))
on('process.run', (_: unknown, e: { argv: readonly string[] }) => {
const argv = [...e.argv]
const inert = nothingToReport(argv)
if (inert) return { value: inert }
runs.push(argv)
const ok = (stdout = '') => ({ value: { exitCode: 0, stdout, stderr: '' } })
const [bin] = argv
Expand Down
22 changes: 17 additions & 5 deletions plugins/altimate-code/hooks/tool-chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect, test } from 'claude-code/testing'
import { expect, mock, test } from 'claude-code/testing'

import { FEATURE_IDS } from './features'
import { register } from './integrations'
import { STATUS_ORDER, statusLineOf } from './status-line'
import { within } from './altimate-cli'
import { nothingToReport } from './inert-features'
import { afterCall, beforeCall, PHASES, type ChainSteps } from './tool-chain'

/** Steps that record each phase they run, in order. */
Expand Down Expand Up @@ -86,15 +87,26 @@ test('the features are listed in the stated order, and the status line joins its
expect(statusLineOf({ integrations: '', runs: '' })).toBeUndefined()
})

test('through the plugin, a Bash call passes unchanged while every feature is a stub', async ($, on) => {
test('through the plugin, a Bash call reaches the tool unchanged and its result comes back with no added context', async ($, on) => {
// The session beneath has nothing to report, so no feature that is built holds a call or adds to one it has no reason to.
mock.clock(on)
on('ui.status', () => ({ value: undefined }))
on('session.root', () => ({ value: '/repo' }))
on('fs.exists', () => ({ value: false }))
on('process.run', (_: unknown, e: { argv: readonly string[] }) => ({ value: { isStdoutTruncated: false, isStderrTruncated: false, ...(nothingToReport(e.argv) ?? { exitCode: 1, stdout: '', stderr: 'no CLI here' }) } }))
const calls: any[] = []
on('tool.call', (_: unknown, e: any) => {
calls.push(e)
return { result: { ok: true } }
})
const bash: any = await $.tool.call({ tool: 'Bash', command: 'dbt run -s orders' })
expect(calls.map(one => one.command ?? one.tool)).toEqual(['dbt run -s orders'])
expect(bash.context ?? []).toEqual([])
// A dbt command may get a note from a feature that watches dbt (what a run would scan); a call no feature
// watches gets nothing. Neither is held or changed.
const dbt: any = await $.tool.call({ tool: 'Bash', command: 'dbt run -s orders' })
const plain: any = await $.tool.call({ tool: 'Bash', command: 'ls models' })
expect(calls.map(one => one.command ?? one.tool)).toEqual(['dbt run -s orders', 'ls models'])
expect(dbt.deny).toBeUndefined()
expect(plain.deny).toBeUndefined()
expect(plain.context ?? []).toEqual([])
})

test('the plugin registers the chain: one tool.call hook for Bash and the file tools, which runs the call it is given', async () => {
Expand Down
Loading