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
225 changes: 104 additions & 121 deletions __tests__/changeset-formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ describe('Change Set Formatter', () => {
ResourceType: 'AWS::DynamoDB::Table',
Replacement: 'True',
Scope: ['Properties'],
BeforeContext: JSON.stringify({
Properties: {
BillingMode: 'PROVISIONED'
}
}),
AfterContext: JSON.stringify({
Properties: {
BillingMode: 'PAY_PER_REQUEST'
}
}),
Details: [
{
Target: {
Expand Down Expand Up @@ -458,164 +468,135 @@ describe('Change Set Formatter', () => {
)
expect(markdown).toContain('**Physical ID:** `my-table-123`')
expect(markdown).toContain('⚠️ **This resource will be replaced**')
expect(markdown).toContain(
'**BillingMode:** `PROVISIONED` → `PAY_PER_REQUEST`'
)
expect(markdown).toContain('```diff')
expect(markdown).toContain('⚠️ Requires recreation: Always')
})

test('diffs Tags arrays correctly', () => {
test('displays AfterContext for Add actions in console output', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Modify',
LogicalResourceId: 'MyParameter',
ResourceType: 'AWS::SSM::Parameter',
Replacement: 'False',
Scope: ['Properties'],
Details: [
{
Target: {
Attribute: 'Properties',
Name: 'Tags',
RequiresRecreation: 'Never',
BeforeValue: JSON.stringify([
{ Key: 'Version', Value: 'v1' },
{ Key: 'Team', Value: 'DevOps' },
{ Key: 'Environment', Value: 'test' }
]),
AfterValue: JSON.stringify([
{ Key: 'Version', Value: 'v2' },
{ Key: 'UpdateType', Value: 'InPlace' },
{ Key: 'Environment', Value: 'production' }
])
}
}
]
Action: 'Add',
LogicalResourceId: 'NewBucket',
ResourceType: 'AWS::S3::Bucket',
AfterContext:
'{"BucketName":"my-bucket","Versioning":{"Status":"Enabled"}}'
}
}
],
totalChanges: 1,
truncated: false
})

const markdown = generateChangeSetMarkdown(changesSummary)
displayChangeSet(changesSummary, 1, true)

expect(markdown).toContain('**Tags:**')
expect(markdown).toContain('**Tags.Environment:** `test` → `production`')
expect(markdown).toContain('**Tags.Team:** `DevOps` → (removed)')
expect(markdown).toContain('**Tags.UpdateType:** (added) → `InPlace`')
expect(markdown).toContain('**Tags.Version:** `v1` → `v2`')
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('Properties:')
)
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('BucketName')
)
})

test('diffs nested objects correctly', () => {
test('displays BeforeContext for Remove actions in console output', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Modify',
LogicalResourceId: 'MyResource',
Action: 'Remove',
LogicalResourceId: 'OldBucket',
ResourceType: 'AWS::S3::Bucket',
BeforeContext: '{"BucketName":"old-bucket"}'
}
}
],
totalChanges: 1,
truncated: false
})

displayChangeSet(changesSummary, 1, true)

expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('Properties:')
)
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('BucketName')
)
})

test('handles invalid JSON in AfterContext gracefully', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Add',
LogicalResourceId: 'NewResource',
ResourceType: 'AWS::Custom::Resource',
Replacement: 'False',
Scope: ['Properties'],
Details: [
{
Target: {
Attribute: 'Properties',
Name: 'Config',
RequiresRecreation: 'Never',
BeforeValue: JSON.stringify({
Setting: 'old',
Nested: { Value: 'a' }
}),
AfterValue: JSON.stringify({
Setting: 'new',
Nested: { Value: 'b' }
})
}
}
]
AfterContext: 'invalid-json{'
}
}
],
totalChanges: 1,
truncated: false
})

const markdown = generateChangeSetMarkdown(changesSummary)
displayChangeSet(changesSummary, 1, true)

expect(markdown).toContain('**Config.Setting:** `old` → `new`')
expect(markdown).toContain('**Config.Nested.Value:** `a` → `b`')
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('invalid-json{')
)
})

test('handles generic arrays as JSON strings', () => {
test('handles invalid JSON in BeforeContext gracefully', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Modify',
LogicalResourceId: 'MyResource',
Action: 'Remove',
LogicalResourceId: 'OldResource',
ResourceType: 'AWS::Custom::Resource',
Replacement: 'False',
Scope: ['Properties'],
Details: [
{
Target: {
Attribute: 'Properties',
Name: 'Items',
RequiresRecreation: 'Never',
BeforeValue: JSON.stringify(['a', 'b']),
AfterValue: JSON.stringify(['a', 'c'])
}
}
]
BeforeContext: 'invalid-json{'
}
}
],
totalChanges: 1,
truncated: false
})

const markdown = generateChangeSetMarkdown(changesSummary)
displayChangeSet(changesSummary, 1, true)

expect(markdown).toContain('**Items:**')
expect(markdown).toContain('["a","b"]')
expect(markdown).toContain('["a","c"]')
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining('invalid-json{')
)
})

test('handles array additions and removals', () => {
test('generates diff view for resources with BeforeContext/AfterContext', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Modify',
LogicalResourceId: 'MyResource',
ResourceType: 'AWS::Custom::Resource',
LogicalResourceId: 'MyTopic',
ResourceType: 'AWS::SNS::Topic',
Replacement: 'False',
Scope: ['Properties'],
Details: [
{
Target: {
Attribute: 'Properties',
Name: 'NewList',
RequiresRecreation: 'Never',
AfterValue: JSON.stringify(['x', 'y'])
}
},
{
Target: {
Attribute: 'Properties',
Name: 'OldList',
RequiresRecreation: 'Never',
BeforeValue: JSON.stringify(['a', 'b'])
}
BeforeContext: JSON.stringify({
Properties: {
DisplayName: 'old-name',
Tags: [{ Key: 'Env', Value: 'dev' }]
}
]
}),
AfterContext: JSON.stringify({
Properties: {
DisplayName: 'new-name',
Tags: [{ Key: 'Env', Value: 'prod' }]
}
})
}
}
],
Expand All @@ -625,36 +606,38 @@ describe('Change Set Formatter', () => {

const markdown = generateChangeSetMarkdown(changesSummary)

expect(markdown).toContain('**NewList:** (added) → `["x","y"]`')
expect(markdown).toContain('**OldList:** `["a","b"]` → (removed)')
expect(markdown).toContain('```diff')
expect(markdown).toContain('-')
expect(markdown).toContain('+')
})

test('handles primitive value additions and removals', () => {
test('shows recreation warnings in diff view', () => {
const changesSummary = JSON.stringify({
changes: [
{
Type: 'Resource',
ResourceChange: {
Action: 'Modify',
LogicalResourceId: 'MyResource',
ResourceType: 'AWS::Custom::Resource',
Replacement: 'False',
Scope: ['Properties'],
LogicalResourceId: 'MyParam',
ResourceType: 'AWS::SSM::Parameter',
Replacement: 'True',
BeforeContext: JSON.stringify({
Properties: {
Name: '/old/path',
Value: 'old'
}
}),
AfterContext: JSON.stringify({
Properties: {
Name: '/new/path',
Value: 'new'
}
}),
Details: [
{
Target: {
Attribute: 'Properties',
Name: 'NewProp',
RequiresRecreation: 'Never',
AfterValue: 'new-value'
}
},
{
Target: {
Attribute: 'Properties',
Name: 'OldProp',
RequiresRecreation: 'Never',
BeforeValue: 'old-value'
Name: 'Name',
RequiresRecreation: 'Always'
}
}
]
Expand All @@ -667,8 +650,8 @@ describe('Change Set Formatter', () => {

const markdown = generateChangeSetMarkdown(changesSummary)

expect(markdown).toContain('**NewProp:** (added) → `new-value`')
expect(markdown).toContain('**OldProp:** `old-value` → (removed)')
expect(markdown).toContain('```diff')
expect(markdown).toContain('⚠️ Requires recreation: Always')
})

test('displays AfterContext for Add actions in console output', () => {
Expand Down
Loading