test: add tests for article metadata extraction
- extractArticleMetadata: 6 tests for title/preview extraction - mapTweetResult: 3 tests for article field inclusion
This commit is contained in:
committed by
Peter Steinberger
parent
cf78fce646
commit
0475a2e1b6
@@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user