diff --git a/packages/cli-kit/src/private/node/content-tokens.test.ts b/packages/cli-kit/src/private/node/content-tokens.test.ts index 99a88ea5eae..7feb3a76dc2 100644 --- a/packages/cli-kit/src/private/node/content-tokens.test.ts +++ b/packages/cli-kit/src/private/node/content-tokens.test.ts @@ -1,4 +1,5 @@ -import {LinkContentToken} from './content-tokens.js' +import {LinkContentToken, LinesDiffContentToken} from './content-tokens.js' +import colors from '../../public/node/colors.js' import {describe, expect, test} from 'vitest' describe('LinkContentToken', () => { @@ -20,3 +21,33 @@ describe('LinkContentToken', () => { expect(got.output()).toEqual(fallback) }) }) + +describe('LinesDiffContentToken', () => { + test('formats added and removed lines correctly', () => { + // Given + const changes = [ + {value: 'same\n', count: 1}, + {value: 'removed\n', count: 1, removed: true}, + {value: 'added\n', count: 1, added: true}, + ] + const token = new LinesDiffContentToken(changes) + + // When + const output = token.output() + + // Then + expect(output).toEqual(['same\n', colors.magenta('- removed\n'), colors.green('+ added\n')]) + }) + + test('handles multiple lines in a single change part', () => { + // Given + const changes = [{value: 'added1\nadded2\n', count: 2, added: true}] + const token = new LinesDiffContentToken(changes) + + // When + const output = token.output() + + // Then + expect(output).toEqual([colors.green('+ added1\n'), colors.green('+ added2\n')]) + }) +}) diff --git a/packages/cli-kit/src/private/node/content-tokens.ts b/packages/cli-kit/src/private/node/content-tokens.ts index 5f2cbb5eee3..affc764eeb3 100644 --- a/packages/cli-kit/src/private/node/content-tokens.ts +++ b/packages/cli-kit/src/private/node/content-tokens.ts @@ -63,27 +63,25 @@ export class JsonContentToken extends ContentToken { export class LinesDiffContentToken extends ContentToken { output(): string[] { - return this.value - .map((part) => { - if (part.added) { - return part.value - .split(/\n/) - .filter((line) => line !== '') - .map((line) => { - return colors.green(`+ ${line}\n`) - }) - } else if (part.removed) { - return part.value - .split(/\n/) - .filter((line) => line !== '') - .map((line) => { - return colors.magenta(`- ${line}\n`) - }) - } else { - return part.value - } - }) - .flat() + return this.value.flatMap((part) => { + if (part.added) { + return part.value + .split(/\n/) + .filter((line) => line !== '') + .map((line) => { + return colors.green(`+ ${line}\n`) + }) + } else if (part.removed) { + return part.value + .split(/\n/) + .filter((line) => line !== '') + .map((line) => { + return colors.magenta(`- ${line}\n`) + }) + } else { + return part.value + } + }) } }