Hotfix: auto-download fallback when Word cold-start drops the open URL

ผู้ใช้จริง (Mac): กด "เปิดใน Word" ตอน Word ยังไม่เปิดค้างอยู่ — Word
cold-start แล้ว URL event หลุด แสดงหน้า Recent แทนเอกสาร (ข้อจำกัด
ms-word:ofe บน macOS; กลไกนี้ผ่าน manual test ตอน Word เปิดอยู่ก่อนแล้ว)

- useVersionOpen.openDocxUrl: หน้าต่างรอ 1.5s → 3s และเมื่อไม่เห็นการ
  สลับแอป → ดาวน์โหลดไฟล์ให้อัตโนมัติ (anchor → presigned attachment)
  + ข้อความแนะนำเปิดจาก Downloads หรือกดซ้ำเมื่อ Word เปิดอยู่แล้ว
  — ใช้ร่วมทุกจุดเปิด docx (version rows, v0, ไฟล์ร่าง) PDF ไม่เกี่ยว
- Tests: timing edge 2999/3000 + href + notice, ไม่ download เมื่อสลับ
  แอปจริง, re-click ยกเลิก timer เก่า (web 59/59 + build)
- Spec FR-15/16 (~3s + auto-download + หมายเหตุ cold-start) +
  acceptance-review เพิ่ม manual check item
- QA reviewer PASS (1 round; minors 3 — แก้ 1 ยอมรับ 2 พร้อมเหตุผล)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Suphonchai Phoonsawat 2026-09-10 12:22:19 +07:00
parent b9b227926d
commit e0558e3cf4
6 changed files with 82 additions and 10 deletions

View file

@ -12,6 +12,13 @@ export function useVersionOpen() {
let generation = 0
let pendingTab: Window | null = null
/**
* รอสังเกตการสลับไป Word (blur/visibilitychange) กี่มิลลิวินาทีก่อนสรุปว่าเปิดไม่สำเร็จ —
* ยาวพอให้ Word cold-start โอกาสเปิดได้ทัน แต่ไม่นานจนผู้ใช้งง (ms-word:ofe บน macOS ตอน Word
* ยังไม่เปิดค้างอยู่มักทำ URL event หลุด → เห็นหน้า Recent แทนเอกสาร — ข้อจำกัดของ platform)
*/
const WORD_SWITCH_WAIT_MS = 3000
function resetOpen() {
generation++
pendingTab?.close()
@ -23,7 +30,18 @@ export function useVersionOpen() {
openNotice.value = null
}
/** เปิด presigned docx URL ใน Word ผ่าน ms-word:ofe พร้อม heuristic เตือนเมื่อไม่เห็นการสลับแอป —
/** ให้เบราว์เซอร์เซฟ URL ลง Downloads (presigned URL ส่ง disposition: attachment อยู่แล้ว —
* หน้าเว็บอยู่ที่เดิม ไม่ navigate ไปไหน) */
function saveToDownloads(url: string) {
const a = document.createElement('a')
a.href = url
document.body.append(a)
a.click()
a.remove()
}
/** เปิด presigned docx URL ใน Word ผ่าน ms-word:ofe — ถ้าไม่เห็นการสลับแอปภายใน WORD_SWITCH_WAIT_MS
* ถือว่าเปิดไม่สำเร็จ: ดาวน์โหลดไฟล์ให้อัตโนมัติ + แจ้งผู้ใช้ (กดซ้ำตอน Word เปิดอยู่แล้วจะสำเร็จ)
* ใช้กับลิงก์ที่ถืออยู่เองได้เลย (ไฟล์ร่าง FR-16: response ของ generate-draft คือ URL แล้ว) */
function openDocxUrl(url: string) {
resetOpen()
@ -34,9 +52,13 @@ export function useVersionOpen() {
window.addEventListener('blur', onBlur)
document.addEventListener('visibilitychange', onVisibility)
const timer = window.setTimeout(() => {
if (!switched) openNotice.value = 'หาก Word ไม่เปิด กรุณาดาวน์โหลดไฟล์แล้วเปิดใน Word'
if (!switched) {
openNotice.value =
'Word ไม่ได้เปิดเอกสารอัตโนมัติ (มักเกิดตอน Word เพิ่งถูกเปิดขึ้นครั้งแรก) — ดาวน์โหลดไฟล์ให้แล้วที่โฟลเดอร์ Downloads ให้เปิดจากที่นั่น หรือกดเปิดใน Word อีกครั้งเมื่อ Word เปิดอยู่แล้ว'
saveToDownloads(url)
}
cleanup()
}, 1500)
}, WORD_SWITCH_WAIT_MS)
cleanup = () => {
window.clearTimeout(timer)
window.removeEventListener('blur', onBlur)

View file

@ -87,11 +87,12 @@ describe('version open lifecycle', () => {
it('offers Word fallback after timeout and removes listeners on unmount', async () => {
const assign = vi.spyOn(window.location, 'assign').mockImplementation(() => {})
const remove = vi.spyOn(window, 'removeEventListener')
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {}) // กัน auto-download 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')
vi.advanceTimersByTime(1500)
vi.advanceTimersByTime(3000)
expect(state.openNotice.value).toBeTruthy()
expect(state.fallbackUrl.value).toBeTruthy()
app.unmount()
@ -99,12 +100,58 @@ describe('version open lifecycle', () => {
})
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')
vi.advanceTimersByTime(1500)
vi.advanceTimersByTime(3000)
expect(state.openNotice.value).toBeTruthy()
})
it('auto-downloads the file to Downloads when Word did not take over within the wait window', () => {
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')
// ยังไม่มีอะไรเกิดก่อนหมดเวลารอ
vi.advanceTimersByTime(2999)
expect(saved).toHaveLength(0)
expect(state.openNotice.value).toBeNull()
// ครบหน้าต่างรอ (ไม่มีการ blur = Word ไม่ได้เปิด) → เซฟไฟล์ให้อัตโนมัติ + แจ้งเหตุผล
vi.advanceTimersByTime(1)
expect(saved).toHaveLength(1)
expect(saved[0].href).toBe('https://files.example/draft.docx?sig=a%2Fb')
expect(state.openNotice.value).toContain('Downloads')
})
it('does not auto-download when the app switch was detected (Word เปิดจริง)', async () => {
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')
window.dispatchEvent(new Event('blur')) // ผู้ใช้สลับไป Word
vi.advanceTimersByTime(3000)
expect(saved).toHaveLength(0)
expect(state.openNotice.value).toBeNull()
})
it('re-click cancels the pending auto-download — only the latest URL is saved once', () => {
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')
vi.advanceTimersByTime(2000)
state.openDocxUrl('https://files.example/second.docx') // resetOpen ยกเลิก timer อันเก่า
vi.advanceTimersByTime(3000)
expect(saved).toHaveLength(1)
expect(saved[0].href).toBe('https://files.example/second.docx')
})
})