feat(ext/sw): GitHost.writeFileCreateOnly() refuses to overwrite

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
adlee-was-taken
2026-04-27 18:06:48 -04:00
parent 98c962796f
commit 86b5941875
4 changed files with 89 additions and 0 deletions

View File

@@ -59,3 +59,53 @@ describe('lastCommit (GitHub)', () => {
expect(await host.lastCommit('manifest.enc')).toBeNull();
});
});
describe('writeFileCreateOnly (Gitea)', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => { fetchSpy = vi.spyOn(globalThis, 'fetch'); });
it('creates when file does not exist', async () => {
fetchSpy
.mockResolvedValueOnce(new Response('', { status: 404 })) // pre-check
.mockResolvedValueOnce(new Response('{}', { status: 201 })); // POST
const host = new GiteaHost('https://git.example.com', 'user/vault', 'tok');
await host.writeFileCreateOnly('manifest.enc', new Uint8Array([1, 2, 3]), 'init');
expect(fetchSpy).toHaveBeenCalledTimes(2);
expect((fetchSpy.mock.calls[1][1] as RequestInit).method).toBe('POST');
});
it('throws when file already exists', async () => {
fetchSpy.mockResolvedValueOnce(new Response(
JSON.stringify({ sha: 'abc' }), { status: 200 },
));
const host = new GiteaHost('https://git.example.com', 'user/vault', 'tok');
await expect(
host.writeFileCreateOnly('manifest.enc', new Uint8Array([1]), 'init'),
).rejects.toThrow(/already exists/);
expect(fetchSpy).toHaveBeenCalledTimes(1); // pre-check only, no write
});
});
describe('writeFileCreateOnly (GitHub)', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => { fetchSpy = vi.spyOn(globalThis, 'fetch'); });
it('throws when file already exists', async () => {
fetchSpy.mockResolvedValueOnce(new Response(
JSON.stringify({ sha: 'abc' }), { status: 200 },
));
const host = new GitHubHost('user/vault', 'tok');
await expect(
host.writeFileCreateOnly('manifest.enc', new Uint8Array([1]), 'init'),
).rejects.toThrow(/already exists/);
});
it('creates when file does not exist', async () => {
fetchSpy
.mockResolvedValueOnce(new Response('', { status: 404 }))
.mockResolvedValueOnce(new Response('{}', { status: 201 }));
const host = new GitHubHost('user/vault', 'tok');
await host.writeFileCreateOnly('manifest.enc', new Uint8Array([1]), 'init');
expect(fetchSpy).toHaveBeenCalledTimes(2);
});
});