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

64 lines
1.7 KiB
TypeScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { RedditService, GithubService } from '../src/platforms/misc.service.js';
function stubFetch(handler: (url: string) => any) {
const real = globalThis.fetch;
(globalThis as any).fetch = async (url: string, _init?: any) => handler(url);
return () => {
globalThis.fetch = real;
};
}
describe('RedditService', () => {
it('maps about.json to profile', async () => {
const restore = stubFetch(async () => ({
status: 200,
ok: true,
json: async () => ({
data: { link_karma: 10, comment_karma: 5, created_utc: 1600000000 },
}),
}));
try {
const out = (await new RedditService().user('spez')) as any;
assert.equal(out.found, true);
assert.equal(out.karma, 15);
assert.equal(out.profileUrl, 'https://www.reddit.com/user/spez');
} finally {
restore();
}
});
it('returns found:false on 404', async () => {
const restore = stubFetch(async () => ({ status: 404, ok: false }));
try {
const out = (await new RedditService().user('nope')) as any;
assert.equal(out.found, false);
} finally {
restore();
}
});
});
describe('GithubService', () => {
it('maps user api to profile', async () => {
const restore = stubFetch(async () => ({
status: 200,
ok: true,
json: async () => ({
name: 'Linus',
followers: 1,
following: 0,
public_repos: 2,
html_url: 'https://github.com/torvalds',
}),
}));
try {
const out = (await new GithubService().user('torvalds')) as any;
assert.equal(out.found, true);
assert.equal(out.name, 'Linus');
} finally {
restore();
}
});
});