|
| 1 | +// Copyright © 2025-2026 OpenVCS Contributors |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +vi.mock('../plugins', () => ({ |
| 6 | + runHook: vi.fn(async () => ({ cancelled: false })), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock('../lib/tauri', () => { |
| 10 | + const invoke = vi.fn(async (cmd: string) => { |
| 11 | + if (cmd === 'commit_patch_and_files') return 'oid-123'; |
| 12 | + return []; |
| 13 | + }); |
| 14 | + return { |
| 15 | + TAURI: { |
| 16 | + invoke, |
| 17 | + listen: vi.fn(), |
| 18 | + }, |
| 19 | + isTauriRuntimeAvailable: () => true, |
| 20 | + assertDesktopRuntime: () => {}, |
| 21 | + __invoke: invoke, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +vi.mock('./repo', () => ({ |
| 26 | + hydrateStatus: vi.fn(async () => {}), |
| 27 | + hydrateCommits: vi.fn(async () => {}), |
| 28 | +})); |
| 29 | + |
| 30 | +let state: typeof import('../state/state').state; |
| 31 | + |
| 32 | +/** Mounts the minimal DOM needed for commit binding. */ |
| 33 | +function mountCommitDom() { |
| 34 | + document.body.innerHTML = ` |
| 35 | + <div id="status"></div> |
| 36 | + <input id="commit-summary" /> |
| 37 | + <textarea id="commit-desc"></textarea> |
| 38 | + <button id="commit-btn"></button> |
| 39 | + `; |
| 40 | +} |
| 41 | + |
| 42 | +beforeEach(async () => { |
| 43 | + vi.resetModules(); |
| 44 | + mountCommitDom(); |
| 45 | + state = (await import('../state/state')).state; |
| 46 | + state.files = [{ path: 'content/posts/2026/05/openvcs-announcement.md', status: '??' }] as any; |
| 47 | + state.selectedFiles = new Set(['content/posts/2026/05/openvcs-announcement.md']); |
| 48 | + state.selectedHunksByFile = { |
| 49 | + 'content/posts/2026/05/openvcs-announcement.md': [0], |
| 50 | + } as any; |
| 51 | + state.selectedLinesByFile = {}; |
| 52 | + state.selectedHunks = []; |
| 53 | + state.diffSelectedFiles = new Set(); |
| 54 | + (state as any).branch = 'main'; |
| 55 | +}); |
| 56 | + |
| 57 | +afterEach(() => { |
| 58 | + document.body.innerHTML = ''; |
| 59 | + vi.restoreAllMocks(); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('bindCommit', () => { |
| 63 | + it('keeps untracked selected files in stage_paths', async () => { |
| 64 | + const { bindCommit } = await import('./diff'); |
| 65 | + const commitSummary = document.getElementById('commit-summary') as HTMLInputElement; |
| 66 | + const commitBtn = document.getElementById('commit-btn') as HTMLButtonElement; |
| 67 | + |
| 68 | + commitSummary.value = 'Add post'; |
| 69 | + commitBtn.disabled = false; |
| 70 | + |
| 71 | + bindCommit(); |
| 72 | + commitBtn.click(); |
| 73 | + |
| 74 | + await Promise.resolve(); |
| 75 | + |
| 76 | + const { __invoke: invoke } = await import('../lib/tauri') as any; |
| 77 | + const call = (invoke as ReturnType<typeof vi.fn>).mock.calls.find((args: unknown[]) => args[0] === 'commit_patch_and_files'); |
| 78 | + expect(call).toBeTruthy(); |
| 79 | + expect(call?.[1]).toMatchObject({ |
| 80 | + summary: 'Add post', |
| 81 | + patch: '', |
| 82 | + files: ['content/posts/2026/05/openvcs-announcement.md'], |
| 83 | + stagePaths: ['content/posts/2026/05/openvcs-announcement.md'], |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments