68 lines · 2.4 KB
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { sha1, sha256, hexEncode, hexDecode, gitObjectHash } from '../sha1.js'; |
| 3 | |
| 4 | const encoder = new TextEncoder(); |
| 5 | |
| 6 | describe('sha1', () => { |
| 7 | it('should hash empty string correctly', async () => { |
| 8 | const result = await sha1(new Uint8Array(0)); |
| 9 | expect(result).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709'); |
| 10 | }); |
| 11 | |
| 12 | it('should hash "hello" correctly', async () => { |
| 13 | const result = await sha1(encoder.encode('hello')); |
| 14 | expect(result).toBe('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'); |
| 15 | }); |
| 16 | |
| 17 | it('should hash "hello world" correctly', async () => { |
| 18 | const result = await sha1(encoder.encode('hello world')); |
| 19 | expect(result).toBe('2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'); |
| 20 | }); |
| 21 | }); |
| 22 | |
| 23 | describe('sha256', () => { |
| 24 | it('should hash empty string correctly', async () => { |
| 25 | const result = await sha256(new Uint8Array(0)); |
| 26 | expect(result).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'); |
| 27 | }); |
| 28 | |
| 29 | it('should hash "hello" correctly', async () => { |
| 30 | const result = await sha256(encoder.encode('hello')); |
| 31 | expect(result).toBe('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'); |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | describe('hexEncode / hexDecode', () => { |
| 36 | it('should round-trip bytes', () => { |
| 37 | const bytes = new Uint8Array([0x00, 0x0f, 0x10, 0xff, 0xab, 0xcd]); |
| 38 | const hex = hexEncode(bytes); |
| 39 | expect(hex).toBe('000f10ffabcd'); |
| 40 | const decoded = hexDecode(hex); |
| 41 | expect(decoded).toEqual(bytes); |
| 42 | }); |
| 43 | |
| 44 | it('should handle empty bytes', () => { |
| 45 | const hex = hexEncode(new Uint8Array(0)); |
| 46 | expect(hex).toBe(''); |
| 47 | expect(hexDecode(hex).length).toBe(0); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | describe('gitObjectHash', () => { |
| 52 | it('should compute correct hash for a blob', async () => { |
| 53 | // This is the SHA-1 of a git blob containing "hello\n" |
| 54 | // Verified: echo -n "hello" | git hash-object --stdin |
| 55 | const content = encoder.encode('hello'); |
| 56 | const hash = await gitObjectHash('blob', content); |
| 57 | // "blob 5\0hello" hashed with SHA-1 |
| 58 | // ce013625 is "hello\n" (with newline); "hello" without newline is: |
| 59 | expect(hash).toBe('b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0'); |
| 60 | }); |
| 61 | |
| 62 | it('should compute correct hash for empty blob', async () => { |
| 63 | const hash = await gitObjectHash('blob', new Uint8Array(0)); |
| 64 | // "blob 0\0" hashed with SHA-1 |
| 65 | expect(hash).toBe('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'); |
| 66 | }); |
| 67 | }); |
| 68 |