From 26822e4f2d62ebcdcdbc0fa9fa2aa2ca8fd92830 Mon Sep 17 00:00:00 2001 From: Christopher Nascone Date: Wed, 6 Mar 2024 19:04:03 -0500 Subject: [PATCH] fully tested --- .../frameResultToFrameMetadata.test.ts.snap | 32 ------- .../utils/frameResultToFrameMetadata.test.ts | 94 ++++++++++--------- framegear/utils/frameResultToFrameMetadata.ts | 4 +- 3 files changed, 51 insertions(+), 79 deletions(-) delete mode 100644 framegear/utils/__snapshots__/frameResultToFrameMetadata.test.ts.snap diff --git a/framegear/utils/__snapshots__/frameResultToFrameMetadata.test.ts.snap b/framegear/utils/__snapshots__/frameResultToFrameMetadata.test.ts.snap deleted file mode 100644 index 6e90c95064..0000000000 --- a/framegear/utils/__snapshots__/frameResultToFrameMetadata.test.ts.snap +++ /dev/null @@ -1,32 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`frameResultToFrameMetadata handles buttons 1`] = ` -{ - "buttons": [ - { - "action": "post", - "label": "henlo", - "target": undefined, - }, - { - "action": "post_redirect", - "label": "henlo", - "target": undefined, - }, - { - "action": "mint", - "label": "henlo", - "target": undefined, - }, - { - "action": "link", - "label": "henlo", - "target": "https://google.com", - }, - ], - "image": "https://images.party/this-is-a-test.apng", - "input": undefined, - "postUrl": undefined, - "state": undefined, -} -`; diff --git a/framegear/utils/frameResultToFrameMetadata.test.ts b/framegear/utils/frameResultToFrameMetadata.test.ts index 74a00e5b99..f3f7297deb 100644 --- a/framegear/utils/frameResultToFrameMetadata.test.ts +++ b/framegear/utils/frameResultToFrameMetadata.test.ts @@ -1,55 +1,57 @@ import { frameResultToFrameMetadata } from './frameResultToFrameMetadata'; -const baseGoodDefinition = { - 'fc:frame': 'vNext', - 'fc:frame:image': 'https://images.party/this-is-a-test.apng', - 'og:image': 'https://images.party/this-is-a-test.apng', -}; - describe('frameResultToFrameMetadata', () => { - it('handles the minimum viable input', () => { - expect(frameResultToFrameMetadata(baseGoodDefinition)).toEqual( - expect.objectContaining({ - image: baseGoodDefinition['og:image'], - }), - ); - }); + const baseResult = { + 'fc:frame:button:1': 'Button 1', + 'fc:frame:button:1:action': 'Action 1', + 'fc:frame:button:1:target': 'Target 1', + 'fc:frame:image': 'Image URL', + 'fc:frame:input': 'Input Text', + 'fc:frame:post_url': 'Post URL', + 'fc:frame:state': JSON.stringify({ key: 'value' }), + 'fc:frame:refresh_period': '10', + }; - it('handles buttons', () => { - expect( - frameResultToFrameMetadata({ - ...baseGoodDefinition, - 'fc:frame:button:1': 'henlo', - 'fc:frame:button:1:action': 'post', - 'fc:frame:button:2': 'henlo', - 'fc:frame:button:2:action': 'post_redirect', - 'fc:frame:button:3': 'henlo', - 'fc:frame:button:3:action': 'mint', - 'fc:frame:button:4': 'henlo', - 'fc:frame:button:4:action': 'link', - 'fc:frame:button:4:target': 'https://google.com', - }), - ).toMatchSnapshot(); - }); + it('should correctly map frame result to frame metadata', () => { + const metadata = frameResultToFrameMetadata(baseResult); - it('handles input text', () => { - expect(frameResultToFrameMetadata({ ...baseGoodDefinition, 'fc:frame:input': 'lmbo' })).toEqual( - expect.objectContaining({ - input: { text: 'lmbo' }, - }), - ); + expect(metadata).toEqual({ + buttons: [ + { + action: 'Action 1', + label: 'Button 1', + target: 'Target 1', + }, + undefined, + undefined, + undefined, + ], + image: 'Image URL', + input: { text: 'Input Text' }, + postUrl: 'Post URL', + state: { key: 'value' }, + refreshPeriod: 10, + }); }); - it('handles state', () => { - expect( - frameResultToFrameMetadata({ - ...baseGoodDefinition, - 'fc:frame:state': JSON.stringify({ status: 'buzzin' }), - }), - ).toEqual( - expect.objectContaining({ - state: { status: 'buzzin' }, - }), - ); + it('should handle missing optional fields', () => { + const result = { ...baseResult }; + delete (result as any)['fc:frame:button:1']; + delete (result as any)['fc:frame:image']; + delete (result as any)['fc:frame:input']; + delete (result as any)['fc:frame:post_url']; + delete (result as any)['fc:frame:state']; + delete (result as any)['fc:frame:refresh_period']; + + const metadata = frameResultToFrameMetadata(result); + + expect(metadata).toEqual({ + buttons: [undefined, undefined, undefined, undefined], + image: undefined, + input: undefined, + postUrl: undefined, + state: undefined, + refreshPeriod: undefined, + }); }); }); diff --git a/framegear/utils/frameResultToFrameMetadata.ts b/framegear/utils/frameResultToFrameMetadata.ts index 28ead4fa4e..e4f29b90d4 100644 --- a/framegear/utils/frameResultToFrameMetadata.ts +++ b/framegear/utils/frameResultToFrameMetadata.ts @@ -15,7 +15,9 @@ export function frameResultToFrameMetadata(result: Record): Fram const input = inputText ? { text: inputText } : undefined; const postUrl = result['fc:frame:post_url']; const rawState = result['fc:frame:state']; + const rawRefreshPeriod = result['fc:frame:refresh_period']; + const refreshPeriod = rawRefreshPeriod ? parseInt(rawRefreshPeriod, 10) : undefined; const state = rawState ? JSON.parse(result['fc:frame:state']) : undefined; - return { buttons: buttons as any, image, input, postUrl, state }; + return { buttons: buttons as any, image, input, postUrl, state, refreshPeriod }; }