// 주소만 보내는 공유(브라우저 공유창 → 안 되면 복사) 검증. 실행: frontend 폴더에서 npm test import assert from 'node:assert/strict' import { test } from 'node:test' import { copyLink, sendLink } from '../src/lib/linkShare.ts' const URL_ = 'https://weather.o2o.kr/type/sonagi' const TITLE = '소나기형 · 나는 무슨 날씨일까?' /** 부른 기록이 남는 가짜 브라우저 */ function fakeNav({ share, write } = {}) { const calls = { share: [], write: [] } const nav = {} if (share) nav.share = async (data) => { calls.share.push(data) return share() } if (write) nav.clipboard = { writeText: async (text) => { calls.write.push(text) return write() }, } return { nav, calls } } const abort = () => { throw new DOMException('Share canceled', 'AbortError') } const broken = () => { throw new Error('NotAllowedError') } test('공유창이 있으면 제목과 주소만 넘기고 share_sheet', async () => { const { nav, calls } = fakeNav({ share: () => {}, write: () => {} }) assert.equal(await sendLink(nav, TITLE, URL_), 'share_sheet') assert.deepEqual(calls.share, [{ title: TITLE, url: URL_ }]) assert.deepEqual(calls.write, []) }) test('공유창을 그냥 닫으면 cancelled 이고 복사도 하지 않는다', async () => { const { nav, calls } = fakeNav({ share: abort, write: () => {} }) assert.equal(await sendLink(nav, TITLE, URL_), 'cancelled') assert.deepEqual(calls.write, []) }) test('공유창이 오류로 실패하면 주소를 복사하고 copy', async (t) => { const warn = t.mock.method(console, 'warn', () => {}) const { nav, calls } = fakeNav({ share: broken, write: () => {} }) assert.equal(await sendLink(nav, TITLE, URL_), 'copy') assert.deepEqual(calls.write, [URL_]) assert.equal(warn.mock.callCount(), 1) }) test('공유창이 없으면 주소를 복사하고 copy', async () => { const { nav, calls } = fakeNav({ write: () => {} }) assert.equal(await sendLink(nav, TITLE, URL_), 'copy') assert.deepEqual(calls.write, [URL_]) }) test('공유창도 클립보드도 없으면 failed', async (t) => { const warn = t.mock.method(console, 'warn', () => {}) assert.equal(await sendLink(fakeNav().nav, TITLE, URL_), 'failed') assert.equal(warn.mock.callCount(), 1) }) test('복사가 막혀 있으면 failed', async (t) => { const warn = t.mock.method(console, 'warn', () => {}) assert.equal(await sendLink(fakeNav({ write: broken }).nav, TITLE, URL_), 'failed') assert.equal(warn.mock.callCount(), 1) }) test('copyLink 는 주소를 복사하고 copy, 안 되면 failed', async (t) => { const warn = t.mock.method(console, 'warn', () => {}) const ok = fakeNav({ write: () => {} }) assert.equal(await copyLink(ok.nav, URL_), 'copy') assert.deepEqual(ok.calls.write, [URL_]) assert.equal(await copyLink(fakeNav().nav, URL_), 'failed') assert.equal(warn.mock.callCount(), 1) })