diff --git a/src/utils/encoding.ts b/src/utils/encoding.ts index d6a2cef..db38eae 100644 --- a/src/utils/encoding.ts +++ b/src/utils/encoding.ts @@ -1,5 +1,5 @@ -import * as iconv from 'iconv-lite'; -import * as jschardet from 'jschardet'; +import iconv from 'iconv-lite'; +import jschardet from 'jschardet'; const regCharset = new RegExp(/charset\s*=\s*["']?([\w-]+)/, 'i'); diff --git a/src/utils/got.ts b/src/utils/got.ts index e08fb3b..e3465c3 100644 --- a/src/utils/got.ts +++ b/src/utils/got.ts @@ -10,9 +10,9 @@ import { readFileSync } from 'node:fs'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); -export let agent: Got.Agents | undefined = undefined; +export let agent: Got.Agents = {}; export function setAgent(_agent: Got.Agents) { - agent = _agent; + agent = _agent || {}; } export type GotOptions = { diff --git a/test/index.ts b/test/index.ts index 5f78227..f92803b 100644 --- a/test/index.ts +++ b/test/index.ts @@ -8,7 +8,7 @@ import * as fs from 'fs'; import fastify from 'fastify'; -import { summaly } from '../built/index.js'; +import { summaly } from '../src/index.js'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import {expect, jest, test, describe, beforeEach, afterEach} from '@jest/globals'; @@ -33,13 +33,12 @@ process.on('unhandledRejection', console.dir); let app: ReturnType; afterEach(() => { - if (app) app.close(); + if (app) return app.close(); }); /* tests below */ test('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', async () => { - console.log('AAAAAAAAAAAAAAAAAAAAAAAA') app = fastify({ logger: true, }); @@ -48,19 +47,17 @@ test('faviconがHTML上で指定されていないが、ルートに存在する }); app.get('/favicon.ico', (_, reply) => reply.status(200)); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.icon).toBe(`${host}/favicon.ico`); }); -/* + test('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', async () => { app = fastify(); app.get('/', (request, reply) => { return reply.send(fs.createReadStream(_dirname + '/htmls/no-favicon.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.icon).toBe(null); @@ -72,7 +69,6 @@ test('titleがcleanupされる', async () => { return reply.send(fs.createReadStream(_dirname + '/htmls/ditry-title.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.title).toBe('Strawberry Pasta'); @@ -89,9 +85,9 @@ describe('Private IP blocking', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html')); }); await app.listen({ port }); - await app.ready(); - expect(summaly(host)).toThrow(); + + expect(() => summaly(host)).toThrow(); }); afterEach(() => { @@ -106,7 +102,6 @@ describe('OGP', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.title).toBe('Strawberry Pasta'); @@ -118,7 +113,6 @@ describe('OGP', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/og-description.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.description).toBe('Strawberry Pasta'); @@ -130,7 +124,6 @@ describe('OGP', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/og-site_name.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.sitename).toBe('Strawberry Pasta'); @@ -142,7 +135,6 @@ describe('OGP', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/og-image.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.thumbnail).toBe('https://himasaku.net/himasaku.png'); @@ -156,7 +148,6 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-title.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.title).toBe('Strawberry Pasta'); @@ -168,7 +159,6 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-description.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.description).toBe('Strawberry Pasta'); @@ -180,7 +170,6 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-image.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.thumbnail).toBe('https://himasaku.net/himasaku.png'); @@ -192,7 +181,6 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/player-peertube-video.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.player.url).toBe('https://example.com/embedurl'); @@ -204,7 +192,6 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-video.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); expect(summary.player.url).toBe('https://example.com/embedurl'); @@ -216,10 +203,8 @@ describe('TwitterCard', () => { return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-image.html')); }); await app.listen({ port }); - await app.ready(); const summary = await summaly(host); - expect(summary.player.url).toBe('https://example.com/embedurl'); + expect(summary.thumbnail).toBe('https://example.com/imageurl'); }); }); -*/ \ No newline at end of file