diff --git a/tests/extract-article-metadata.test.ts b/tests/extract-article-metadata.test.ts new file mode 100644 index 0000000..8bbb476 --- /dev/null +++ b/tests/extract-article-metadata.test.ts @@ -0,0 +1,88 @@ +import { describe, expect, it } from 'vitest'; +import type { GraphqlTweetResult } from '../src/lib/twitter-client-types.js'; +import { extractArticleMetadata } from '../src/lib/twitter-client-utils.js'; + +describe('extractArticleMetadata', () => { + it('returns undefined when no result', () => { + expect(extractArticleMetadata(undefined)).toBeUndefined(); + }); + + it('returns undefined when no article field', () => { + const result: GraphqlTweetResult = { + rest_id: '123', + legacy: { full_text: 'Hello world' }, + }; + expect(extractArticleMetadata(result)).toBeUndefined(); + }); + + it('returns undefined when article has no title', () => { + const result: GraphqlTweetResult = { + rest_id: '123', + article: { + article_results: { + result: {}, + }, + }, + }; + expect(extractArticleMetadata(result)).toBeUndefined(); + }); + + it('extracts title from article_results.result', () => { + const result: GraphqlTweetResult = { + rest_id: '123', + article: { + article_results: { + result: { + title: 'Building Context Graphs for GTM', + }, + }, + }, + }; + + const metadata = extractArticleMetadata(result); + expect(metadata).toEqual({ + title: 'Building Context Graphs for GTM', + previewText: undefined, + }); + }); + + it('extracts title and preview_text from article_results.result', () => { + const result: GraphqlTweetResult = { + rest_id: '123', + article: { + article_results: { + result: { + title: 'Building Context Graphs for GTM', + preview_text: + 'Foundation Capital recently argued that one of the next trillion-dollar opportunities in AI will come from context graphs.', + }, + }, + }, + } as GraphqlTweetResult; + + const metadata = extractArticleMetadata(result); + expect(metadata).toEqual({ + title: 'Building Context Graphs for GTM', + previewText: + 'Foundation Capital recently argued that one of the next trillion-dollar opportunities in AI will come from context graphs.', + }); + }); + + it('falls back to article.title when article_results.result.title missing', () => { + const result: GraphqlTweetResult = { + rest_id: '123', + article: { + title: 'Fallback Title', + article_results: { + result: {}, + }, + }, + }; + + const metadata = extractArticleMetadata(result); + expect(metadata).toEqual({ + title: 'Fallback Title', + previewText: undefined, + }); + }); +}); diff --git a/tests/map-tweet-article.test.ts b/tests/map-tweet-article.test.ts new file mode 100644 index 0000000..f20d3aa --- /dev/null +++ b/tests/map-tweet-article.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it } from 'vitest'; +import type { GraphqlTweetResult } from '../src/lib/twitter-client-types.js'; +import { mapTweetResult } from '../src/lib/twitter-client-utils.js'; + +describe('mapTweetResult article', () => { + const makeUserResult = () => ({ + core: { + user_results: { + result: { + rest_id: 'u1', + legacy: { screen_name: 'alice', name: 'Alice' }, + }, + }, + }, + }); + + it('includes extracted article metadata on mapped tweet', () => { + const result: GraphqlTweetResult = { + rest_id: '1', + legacy: { + full_text: 'Check out my article', + }, + article: { + article_results: { + result: { + title: 'Building Context Graphs for GTM', + preview_text: 'Foundation Capital recently argued...', + }, + }, + }, + ...makeUserResult(), + } as GraphqlTweetResult; + + const mapped = mapTweetResult(result, 0); + expect(mapped).toBeDefined(); + expect(mapped?.article).toEqual({ + title: 'Building Context Graphs for GTM', + previewText: 'Foundation Capital recently argued...', + }); + }); + + it('omits article when none present', () => { + const result: GraphqlTweetResult = { + rest_id: '2', + legacy: { full_text: 'no article' }, + ...makeUserResult(), + }; + + const mapped = mapTweetResult(result, 0); + expect(mapped?.article).toBeUndefined(); + }); + + it('includes article with only title when no preview_text', () => { + const result: GraphqlTweetResult = { + rest_id: '3', + legacy: { full_text: 'article link' }, + article: { + article_results: { + result: { + title: 'My Article Title', + }, + }, + }, + ...makeUserResult(), + }; + + const mapped = mapTweetResult(result, 0); + expect(mapped?.article).toEqual({ + title: 'My Article Title', + previewText: undefined, + }); + }); +});