feat(ext/sw): GitHost.writeFileCreateOnly() refuses to overwrite
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user