Skip to content

src/discord-attachments.node.test.ts

61 lines · 1.5 KB · TypeScript
Raw
import assert from 'node:assert/strict'
import test from 'node:test'
import { buildImageWakePrompt, collectDiscordImages } from './discord-attachments.ts'

test('collects image attachments from a raw MESSAGE_CREATE', () => {
	const images = collectDiscordImages({
		id: '1',
		channel_id: '1542616423845728337',
		content: '',
		attachments: [
			{
				id: 'att1',
				filename: 'shot.png',
				content_type: 'image/png',
				url: 'https://cdn.discordapp.com/attachments/1/2/shot.png',
			},
			{
				id: 'att2',
				filename: 'notes.txt',
				content_type: 'text/plain',
				url: 'https://cdn.discordapp.com/attachments/1/2/notes.txt',
			},
		],
	})
	assert.equal(images.length, 1)
	assert.equal(images[0]?.filename, 'shot.png')
})

test('finds attachments nested in a subscription wrapper', () => {
	const images = collectDiscordImages({
		event: '@kentcdodds/discord.message.created',
		payload: {
			event: {
				attachments: [
					{
						filename: 'photo.webp',
						url: 'https://cdn.discordapp.com/attachments/9/8/photo.webp',
					},
				],
			},
		},
	})
	assert.equal(images[0]?.filename, 'photo.webp')
})

test('Imogen prompt is an alt-text contract even with empty Discord content', () => {
	const prompt = buildImageWakePrompt({
		botName: 'Imogen',
		content: '',
		images: [
			{
				url: 'https://cdn.discordapp.com/attachments/1/2/shot.png',
				filename: 'shot.png',
				contentType: 'image/png',
			},
		],
	})
	assert.match(prompt, /one brief copyable alt block/)
	assert.match(prompt, /shot\.png/)
	assert.equal(prompt.includes('hello'), false)
})