eCourt-Doc-Edit/web/test/useVersionOpen.spec.ts
Suphonchai Phoonsawat 4f54af4e24 v9.2.1: re-enable ms-word on all platforms + real filenames in URLs
User clarified: with the new open-link URLs (https + no query string +
ending .docx), pressing the button DID open the file in Word 16.101+
on their Mac — only the OLD URL forms (http, query strings) were
ignored. The previous disable-on-Mac was based on terminal tests that
hit the old forms / cold-start races, so it removed working behavior.

- useVersionOpen: fireWordScheme = openInWord !== 'off' (default ''
  and 'on' fire on ALL platforms; only explicit 'off' is download-only)
- Notice updated: "กำลังเปิดไฟล์ใน Word ให้อัตโนมัติ — หากไม่เปิด
  ไฟล์อยู่ที่โฟลเดอร์ Downloads แล้ว" (covers cold-start drop)
- Docs corrected with v9.2.1 header (old forms were the broken ones;
  new form confirmed working via real usage) + correction markers on
  the v9.2 test record + CLAUDE.md rule 13 rewritten
- web 60/60 + build; QA reviewer FAIL round 1 (5 doc/comment minors,
  all fixed) → PASS round 2

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-10 14:33:12 +07:00

153 lines
8.1 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: ยิง ms-word คู่กัน (toggle ไม่มี platform logic แล้ว — แต่ละ 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 คู่กัน — Word เปิดไฟล์ให้อัตโนมัติ (ถ้า cold-start หลุด ไฟล์สำรองก็อยู่ Downloads แล้ว)
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 เลย — ดาวน์โหลดอย่างเดียว', () => {
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).toContain('Downloads') // ข้อความชี้ไปที่ Downloads อย่างเดียว
})
it('default (auto) ยิง ms-word ทุกแพลตฟอร์ม รวมถึง Mac — open-link แบบใหม่ (https + ไม่มี query + จบ .docx) เปิดได้จริงบน Word 16.101+', () => {
vi.spyOn(window.navigator, 'platform', 'get').mockReturnValue('MacIntel')
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).toHaveBeenCalledWith('ms-word:ofe|u|https://files.example/draft.docx')
expect(state.openNotice.value).toContain('อัตโนมัติ')
})
})