import assert from 'node:assert/strict' import { readFileSync, readdirSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import test from 'node:test' import { buildLinkFacets } from './facets.ts' import { APP_PASSWORD_SECRET, APP_PASSWORD_SETUP_URL, AUTH_HOST, PDS_HOST_PATTERN, } from './setup.ts' import { withCloudinaryFit } from './thumbnail.ts' const srcDir = dirname(fileURLToPath(import.meta.url)) const repoRoot = join(srcDir, '..') test('prefilled secret URL names the app password and required hosts', () => { assert.match(APP_PASSWORD_SETUP_URL, /name=blueskyAppPassword/) assert.match(APP_PASSWORD_SETUP_URL, /allowedHosts=bsky\.social,\*\.bsky\.network/) assert.match(APP_PASSWORD_SETUP_URL, /scope=user/) assert.equal(APP_PASSWORD_SECRET, 'blueskyAppPassword') assert.equal(AUTH_HOST, 'bsky.social') assert.equal(PDS_HOST_PATTERN, '*.bsky.network') }) test('source and docs do not hard-code a Kent handle', () => { const files = [ ...readdirSync(srcDir).map((name) => join(srcDir, name)), join(repoRoot, 'README.md'), join(repoRoot, 'package.json'), ] for (const file of files) { const text = readFileSync(file, 'utf8') assert.doesNotMatch(text, /kentcdodds\.com/i, file) assert.doesNotMatch(text, /@kentcdodds\/bluesky/, file) } }) test('buildLinkFacets uses UTF-8 byte indexes and strips trailing punctuation', () => { const text = 'See https://example.com/path, and https://bsky.app/about' const facets = buildLinkFacets(text) assert.equal(facets.length, 2) assert.equal(facets[0].features[0].uri, 'https://example.com/path') assert.equal(facets[0].index.byteStart, new TextEncoder().encode('See ').byteLength) assert.equal( facets[0].index.byteEnd, new TextEncoder().encode('See https://example.com/path').byteLength, ) assert.equal(facets[1].features[0].uri, 'https://bsky.app/about') }) test('withCloudinaryFit only rewrites Cloudinary image delivery URLs', () => { assert.equal(withCloudinaryFit('https://example.com/thumb.jpg'), null) const fitted = withCloudinaryFit( 'https://res.cloudinary.com/demo/image/upload/v123/sample.jpg', ) assert.match(String(fitted), /w_1280,c_limit,q_auto:good,f_jpg/) }) test('app password is a raw credential slot and never a fetch placeholder', () => { const manifest = JSON.parse(readFileSync(join(repoRoot, 'package.json'), 'utf8')) const slot = manifest.kody.credentials[APP_PASSWORD_SECRET] assert.equal(slot.kind, 'secret') assert.equal(slot.raw, true) for (const name of readdirSync(srcDir)) { const text = readFileSync(join(srcDir, name), 'utf8') assert.doesNotMatch(text, /\{\{secret:/, name) } })