socmind/test/twitter.test.ts
2026-09-11 23:33:20 -04:00

100 lines
2.8 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { TwitterService } from '../src/platforms/twitter.service.js';
function stubTwitter() {
const real = globalThis.fetch;
(globalThis as any).fetch = async (url: string, _init?: any) => {
if (url.includes('guest/activate')) {
return { ok: true, json: async () => ({ guest_token: 'GUEST' }) };
}
if (url.includes('UserByScreenName')) {
return {
ok: true,
json: async () => ({
data: {
user: {
result: {
legacy: {
name: 'NASA',
description: 'space',
followers_count: 10,
friends_count: 1,
statuses_count: 5,
verified: false,
created_at: 'yesterday',
location: 'DC',
url: null,
},
},
},
},
}),
};
}
if (url.includes('UserTweets')) {
return {
ok: true,
json: async () => ({
data: {
user: {
result: {
timeline: {
timeline: {
instructions: [
{
entries: [
{
content: {
itemContent: {
tweet_results: {
result: {
legacy: { id_str: '1', full_text: 'hello world' },
},
},
},
},
},
],
},
],
},
},
},
},
},
}),
};
}
throw new Error(`unexpected url ${url}`);
};
return () => {
globalThis.fetch = real;
};
}
describe('TwitterService', () => {
it('maps UserByScreenName legacy to profile', async () => {
const restore = stubTwitter();
try {
const out = (await new TwitterService().profile('nasa')) as any;
assert.equal(out.found, true);
assert.equal(out.name, 'NASA');
assert.equal(out.followers, 10);
assert.equal(out.profileUrl, 'https://x.com/nasa');
} finally {
restore();
}
});
it('extracts tweet texts from timeline instructions', async () => {
const restore = stubTwitter();
try {
const out = (await new TwitterService().tweets('nasa', 5)) as any;
assert.equal(out.tweets.length, 1);
assert.equal(out.tweets[0].text, 'hello world');
} finally {
restore();
}
});
});