Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Each file in `src/commands/` exports a `registerXxxCommand(program)` function th

- **api.ts**: Single `apiRequest<T>(path, body)` function. All Outline API calls are POST with Bearer auth. Wraps requests in spinner with per-endpoint messages (SPINNER_MESSAGES map).
- **auth.ts**: Config stored at `~/.config/outline-cli/config.json`. Token/URL resolution: env var → config file → default.
- **output.ts**: `outputItem()` and `outputList()` handle three output modes (human/json/ndjson). Commands define a `humanFormatter` function and an `essentialKeys` array for JSON field filtering.
- **output.ts**: `outputItem()` and `outputList()` handle three output modes (human/json/ndjson). Commands define a `humanFormatter` function and an `essentialKeys` array for JSON field filtering. `formatError` / `formatErrorJson` accept either positional `(code, message, hints?)` or a `BaseCliError` instance — errors thrown by `@doist/cli-core` helpers (e.g. the delegated `changelog` command) route through the top-level `parseAsync().catch` in `src/index.ts`, which dispatches via `isJsonMode()`.
- **spinner.ts**: `withSpinner(opts, fn)` wrapper using yocto-spinner. Auto-disables on non-TTY, JSON output, CI, `--no-spinner` flag.
- **markdown.ts**: Terminal markdown rendering via marked + marked-terminal.

Expand Down
50 changes: 50 additions & 0 deletions src/__tests__/changelog-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Command } from 'commander'
import { describe, expect, it } from 'vitest'
import { registerChangelogCommand } from '../commands/changelog.js'
import { BaseCliError } from '../lib/errors.js'
import { formatError, formatErrorJson } from '../lib/output.js'

describe('changelog command end-to-end', () => {
it('rejects with BaseCliError(INVALID_TYPE) when --count is not a number', async () => {
const program = new Command()
program.exitOverride()
registerChangelogCommand(program)

await expect(
program.parseAsync(['node', 'ol', 'changelog', '-n', 'abc']),
).rejects.toBeInstanceOf(BaseCliError)

const err = await program
.parseAsync(['node', 'ol', 'changelog', '-n', 'abc'])
.catch((e: Error) => e)
expect(err).toBeInstanceOf(BaseCliError)
expect((err as BaseCliError).code).toBe('INVALID_TYPE')
})

it('formats the rejected BaseCliError through formatError (human)', async () => {
const program = new Command()
program.exitOverride()
registerChangelogCommand(program)

const err = await program
.parseAsync(['node', 'ol', 'changelog', '-n', 'abc'])
.catch((e: Error) => e)
expect(err).toBeInstanceOf(BaseCliError)
const out = formatError(err as BaseCliError)
expect(out).toContain('Error: INVALID_TYPE')
expect(out).toContain('Count must be a positive integer')
})

it('formats the rejected BaseCliError through formatErrorJson', async () => {
const program = new Command()
program.exitOverride()
registerChangelogCommand(program)

const err = await program
.parseAsync(['node', 'ol', 'changelog', '-n', 'abc'])
.catch((e: Error) => e)
const parsed = JSON.parse(formatErrorJson(err as BaseCliError))
expect(parsed.error.code).toBe('INVALID_TYPE')
expect(parsed.error.message).toBe('Count must be a positive integer')
})
})
175 changes: 28 additions & 147 deletions src/__tests__/changelog.test.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,42 @@
import { basename } from 'node:path'
import { Command } from 'commander'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'

vi.mock('node:fs/promises')
const { registerCoreChangelogCommand } = vi.hoisted(() => ({
registerCoreChangelogCommand: vi.fn(),
}))

import { readFile } from 'node:fs/promises'
import { registerChangelogCommand } from '../commands/changelog.js'

const mockReadFile = vi.mocked(readFile)

const SAMPLE_CHANGELOG = `# Changelog

## [1.5.0](https://example.com) (2026-03-15)

### Features

* feature five

## [1.4.0](https://example.com) (2026-03-14)

### Features

* feature four

## [1.3.0](https://example.com) (2026-03-13)

### Bug Fixes

* fix three

## [1.2.0](https://example.com) (2026-03-12)
vi.mock('@doist/cli-core/commands', () => ({
registerChangelogCommand: registerCoreChangelogCommand,
}))

### Features

* feature two

## [1.1.0](https://example.com) (2026-03-11)

### Features

* feature one

## [1.0.0](https://example.com) (2026-03-10)

### Features

* initial release
`

function createProgram() {
const program = new Command()
program.exitOverride()
registerChangelogCommand(program)
return program
}

describe('changelog command', () => {
let consoleSpy: ReturnType<typeof vi.spyOn>
let consoleErrorSpy: ReturnType<typeof vi.spyOn>
import packageJson from '../../package.json' with { type: 'json' }
import { registerChangelogCommand } from '../commands/changelog.js'

describe('changelog wrapper', () => {
beforeEach(() => {
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
process.exitCode = undefined
})

afterEach(() => {
vi.restoreAllMocks()
process.exitCode = undefined
registerCoreChangelogCommand.mockReset()
})

it('shows last 5 versions by default', async () => {
mockReadFile.mockResolvedValue(SAMPLE_CHANGELOG)

const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog'])

expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.5.0'))
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.1.0'))
// Should show "view full changelog" link since there are 6 versions
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('View full changelog'))
})

it('includes latest version when changelog has no preamble', async () => {
const noPreambleChangelog = `## [2.0.0](https://example.com) (2026-03-20)

### Features

* new major version

## [1.5.0](https://example.com) (2026-03-15)

### Features

* feature five
`
mockReadFile.mockResolvedValue(noPreambleChangelog)

const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog'])

const output = consoleSpy.mock.calls[0][0] as string
expect(output).toContain('2.0.0')
expect(output).toContain('1.5.0')
})

it('respects --count option', async () => {
mockReadFile.mockResolvedValue(SAMPLE_CHANGELOG)

const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog', '-n', '2'])

const output = consoleSpy.mock.calls[0][0] as string
expect(output).toContain('1.5.0')
expect(output).toContain('1.4.0')
expect(output).not.toContain('1.3.0')
})

it('handles fewer entries than requested', async () => {
const shortChangelog = `# Changelog

## [1.1.0](https://example.com) (2026-03-11)

### Features

* only version
`
mockReadFile.mockResolvedValue(shortChangelog)

const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog'])

expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.1.0'))
// Should NOT show "view full changelog" link since all versions are shown
expect(consoleSpy).not.toHaveBeenCalledWith(expect.stringContaining('View full changelog'))
})

it('handles missing changelog file', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'))

const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog'])
it('delegates to @doist/cli-core/commands with the expected config', () => {
const program = new Command()
registerChangelogCommand(program)

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining('Could not read changelog file'),
)
expect(process.exitCode).toBe(1)
expect(registerCoreChangelogCommand).toHaveBeenCalledTimes(1)
const [passedProgram, config] = registerCoreChangelogCommand.mock.calls[0]
expect(passedProgram).toBe(program)
expect(basename(config.path)).toBe('CHANGELOG.md')
expect(config.repoUrl).toBe('https://github.com/Doist/outline-cli')
expect(config.version).toBe(packageJson.version)
expect(config.bulletMarkers).toEqual(['*', '-'])
})

it('handles invalid count', async () => {
const program = createProgram()
await program.parseAsync(['node', 'ol', 'changelog', '-n', 'abc'])
it('derives repoUrl from package.json (strips .git / git+ prefix)', () => {
const program = new Command()
registerChangelogCommand(program)

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining('Count must be a positive number'),
)
expect(process.exitCode).toBe(1)
const [, config] = registerCoreChangelogCommand.mock.calls[0]
expect(config.repoUrl).not.toMatch(/\.git$/)
expect(config.repoUrl).not.toMatch(/^git\+/)
})
})
40 changes: 39 additions & 1 deletion src/__tests__/output.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { formatError, getOutputOptions, outputItem, outputList } from '../lib/output.js'
import { BaseCliError } from '../lib/errors.js'
import {
formatError,
formatErrorJson,
getOutputOptions,
outputItem,
outputList,
} from '../lib/output.js'

describe('output', () => {
let logs: string[]
Expand Down Expand Up @@ -87,5 +94,36 @@ describe('output', () => {
expect(result).toContain('No hints provided')
expect(result).not.toContain(' - ')
})

it('formats a cli-core CliError instance (code, message, hints)', () => {
const err = new BaseCliError('FILE_READ_ERROR', 'Could not read changelog file', {
hints: ['Check the file path'],
})
const result = formatError(err)
expect(result).toContain('Error: FILE_READ_ERROR')
expect(result).toContain('Could not read changelog file')
expect(result).toContain('Check the file path')
})
})

describe('formatErrorJson', () => {
Comment thread
scottlovegrove marked this conversation as resolved.
it('serializes a cli-core CliError instance', () => {
const err = new BaseCliError('INVALID_TYPE', 'Count must be a positive integer')
const parsed = JSON.parse(formatErrorJson(err))
expect(parsed).toEqual({
error: {
code: 'INVALID_TYPE',
message: 'Count must be a positive integer',
hints: undefined,
},
})
})

it('serializes from positional args', () => {
const parsed = JSON.parse(formatErrorJson('CODE', 'msg', ['hint']))
expect(parsed).toEqual({
error: { code: 'CODE', message: 'msg', hints: ['hint'] },
})
})
})
})
Loading
Loading