- Web: ยิง ms-word:ofe คู่กับการดาวน์โหลดเฉพาะ non-Mac โดย default — บน Mac ไม่ยิงเพราะ Word 16.101+ เมิน URL แล้วเด้งหน้าเลือกไฟล์ ทุกครั้งจนรำคาญ (override ด้วย NUXT_PUBLIC_OPEN_IN_WORD=on|off) - CLAUDE.md: จดสูตร sideload บน Mac — ปิด Word ก่อน npm start (Word อ่าน manifest ตอน startup เท่านั้น), ปล่อย dev server รันทิ้ง, แก้โค้ดใช้ hot reload - Spec/plan sync + web 60/60 + build; QA reviewer PASS (1 round, minors 3 — แก้ 2 ปฏิเสธ 1 พร้อมเหตุผล) Co-Authored-By: Claude Code <noreply@anthropic.com>
159 lines
8.3 KiB
TypeScript
159 lines
8.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { createApp, type App } from 'vue'
|
|
import { useVersionOpen } from '../app/composables/useVersionOpen'
|
|
|
|
let app: App
|
|
let host: HTMLDivElement
|
|
const apiJson = vi.fn()
|
|
const replace = vi.fn()
|
|
const close = vi.fn()
|
|
let tab: { opener: unknown; closed: boolean; location: { replace: typeof replace }; close: typeof close }
|
|
|
|
function mount() {
|
|
let result!: ReturnType<typeof useVersionOpen>
|
|
host = document.createElement('div')
|
|
document.body.append(host)
|
|
app = createApp({ setup() { result = useVersionOpen(); return () => null } })
|
|
app.mount(host)
|
|
return result
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.stubGlobal('useApi', () => ({ apiJson }))
|
|
// default: auto — happy-dom ไม่ใช่ Mac UA จึงยิง ms-word ควบคู่ (แต่ละ test ที่ต้องการก็ stub ทับได้)
|
|
vi.stubGlobal('useRuntimeConfig', () => ({ public: { openInWord: '' } }))
|
|
tab = { opener: {}, closed: false, location: { replace }, close }
|
|
vi.spyOn(window, 'open').mockReturnValue(tab as unknown as Window)
|
|
})
|
|
afterEach(() => {
|
|
app?.unmount()
|
|
host?.remove()
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllGlobals()
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('version open lifecycle', () => {
|
|
it('reserves PDF tab synchronously, navigates after auth, leaves completed PDF open on unmount', async () => {
|
|
let resolve!: (value: unknown) => void
|
|
apiJson.mockReturnValue(new Promise(r => { resolve = r }))
|
|
const state = mount()
|
|
const pending = state.openVersion('d', 'v', 'pdf')
|
|
expect(window.open).toHaveBeenCalledWith('about:blank', '_blank')
|
|
expect(tab.opener).toBeNull()
|
|
expect(state.openingVersionId.value).toBe('v')
|
|
resolve({ url: 'https://files.example/v.pdf' })
|
|
await pending
|
|
expect(replace).toHaveBeenCalledWith('https://files.example/v.pdf')
|
|
expect(state.fallbackUrl.value).toBe('https://files.example/v.pdf')
|
|
app.unmount()
|
|
expect(close).not.toHaveBeenCalled()
|
|
})
|
|
it('keeps a fallback when popup is blocked', async () => {
|
|
vi.mocked(window.open).mockReturnValue(null)
|
|
apiJson.mockResolvedValue({ url: 'https://files.example/v.pdf' })
|
|
const state = mount()
|
|
await state.openVersion('d', 'v', 'pdf')
|
|
expect(state.openNotice.value).toBeTruthy()
|
|
expect(state.fallbackUrl.value).toBe('https://files.example/v.pdf')
|
|
})
|
|
it('closes pending tab immediately on reset and ignores a late response', async () => {
|
|
let resolve!: (value: unknown) => void
|
|
apiJson.mockReturnValue(new Promise(r => { resolve = r }))
|
|
const state = mount()
|
|
const pending = state.openVersion('d', 'v', 'pdf')
|
|
state.resetOpen()
|
|
expect(close).toHaveBeenCalledTimes(1)
|
|
resolve({ url: 'https://files.example/v.pdf' })
|
|
await pending
|
|
expect(replace).not.toHaveBeenCalled()
|
|
expect(state.fallbackUrl.value).toBeNull()
|
|
})
|
|
it('closes reserved tab and reports request failure', async () => {
|
|
apiJson.mockRejectedValue(new Error('offline'))
|
|
const state = mount()
|
|
await state.openVersion('d', 'v', 'pdf')
|
|
expect(close).toHaveBeenCalled()
|
|
expect(state.downloadError.value).toBe('offline')
|
|
expect(state.openingVersionId.value).toBeNull()
|
|
})
|
|
it('handles synchronous popup errors and restores controls', async () => {
|
|
vi.mocked(window.open).mockImplementation(() => { throw new Error('popup error') })
|
|
const state = mount()
|
|
await state.openVersion('d', 'v', 'pdf')
|
|
expect(state.downloadError.value).toBe('popup error')
|
|
expect(state.openingVersionId.value).toBeNull()
|
|
})
|
|
it('openVersion (docx) ดาวน์โหลดทันที + ยิง ms-word + ตั้ง notice และเคลียร์ตอน unmount', async () => {
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {}) // กัน download-first click จริง
|
|
apiJson.mockResolvedValue({ url: 'https://files.example/v.docx?signature=a%2Fb' })
|
|
const state = mount()
|
|
await state.openVersion('d', 'v')
|
|
expect(assign).toHaveBeenCalledWith('ms-word:ofe|u|https://files.example/v.docx?signature=a%2Fb')
|
|
expect(state.openNotice.value).toBeTruthy()
|
|
expect(state.fallbackUrl.value).toBeTruthy()
|
|
app.unmount()
|
|
})
|
|
it('openDocxUrl opens a URL the caller already holds (ไฟล์ร่าง FR-16) without extra request', async () => {
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
|
const state = mount()
|
|
state.openDocxUrl('https://files.example/draft.docx?sig=a%2Fb')
|
|
expect(apiJson).not.toHaveBeenCalled()
|
|
expect(assign).toHaveBeenCalledWith('ms-word:ofe|u|https://files.example/draft.docx?sig=a%2Fb')
|
|
expect(state.fallbackUrl.value).toBe('https://files.example/draft.docx?sig=a%2Fb')
|
|
expect(state.openNotice.value).toBeTruthy()
|
|
})
|
|
it('download-first: ดาวน์โหลดไฟล์ทันทีที่กด (ไม่ต้องรอ heuristic — Word 16.101+ บน Mac โดนหลอกด้วยหน้าเลือกไฟล์)', () => {
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
const saved: HTMLAnchorElement[] = []
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function clicked(this: HTMLAnchorElement) {
|
|
saved.push(this)
|
|
})
|
|
const state = mount()
|
|
state.openDocxUrl('https://files.example/draft.docx?sig=a%2Fb')
|
|
expect(saved).toHaveLength(1)
|
|
expect(saved[0].href).toBe('https://files.example/draft.docx?sig=a%2Fb')
|
|
expect(state.openNotice.value).toContain('Downloads')
|
|
// ยังยิง ms-word ควบคู่แบบ best-effort เผื่อเครื่องที่ scheme ยังใช้ได้ (Windows / Word รุ่นก่อน)
|
|
expect(assign).toHaveBeenCalledWith('ms-word:ofe|u|https://files.example/draft.docx?sig=a%2Fb')
|
|
})
|
|
it('re-click downloads again with the latest URL', () => {
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
const saved: HTMLAnchorElement[] = []
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function clicked(this: HTMLAnchorElement) {
|
|
saved.push(this)
|
|
})
|
|
const state = mount()
|
|
state.openDocxUrl('https://files.example/first.docx')
|
|
state.openDocxUrl('https://files.example/second.docx') // resetOpen เคลียร์ notice เดิมแล้วดาวน์โหลดใหม่
|
|
expect(saved).toHaveLength(2)
|
|
expect(saved[1].href).toBe('https://files.example/second.docx')
|
|
expect(state.fallbackUrl.value).toBe('https://files.example/second.docx')
|
|
})
|
|
it('OPEN_IN_WORD=off: ไม่ยิง ms-word เลย — ดาวน์โหลดอย่างเดียว (Word 16.101+ บน Mac เด้งหน้าเลือกไฟล์ทุกครั้ง)', () => {
|
|
vi.stubGlobal('useRuntimeConfig', () => ({ public: { openInWord: 'off' } }))
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
|
const state = mount()
|
|
state.openDocxUrl('https://files.example/draft.docx')
|
|
expect(assign).not.toHaveBeenCalled()
|
|
expect(state.openNotice.value).not.toContain('อัตโนมัติ') // ข้อความไม่เกี้ยวเรื่องเปิดอัตโนมัติ
|
|
})
|
|
it('auto บน Mac: ไม่ยิง ms-word (default), override on แล้วยิงแม้บน Mac', () => {
|
|
vi.spyOn(window.navigator, 'platform', 'get').mockReturnValue('MacIntel')
|
|
let state = mount() // default '' → auto → Mac = ไม่ยิง
|
|
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
|
state.openDocxUrl('https://files.example/draft.docx')
|
|
expect(assign).not.toHaveBeenCalled()
|
|
|
|
app.unmount()
|
|
host.remove()
|
|
vi.stubGlobal('useRuntimeConfig', () => ({ public: { openInWord: 'on' } }))
|
|
state = mount()
|
|
state.openDocxUrl('https://files.example/draft.docx')
|
|
expect(assign).toHaveBeenCalledWith('ms-word:ofe|u|https://files.example/draft.docx')
|
|
})
|
|
})
|