Skip to content

Commit

Permalink
Test invalid gguf file, Coverage 100%! 💯
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Apr 22, 2024
1 parent 4a4bb93 commit 66bf169
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![workflow status](https://github.com/hyparam/hyllama/actions/workflows/ci.yml/badge.svg)](https://github.com/hyparam/hyllama/actions)
[![mit license](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![dependencies](https://img.shields.io/badge/Dependencies-0-blueviolet)
![dependencies](https://img.shields.io/badge/Coverage-100%-darkred)

Javascript parser for [llama.cpp](https://github.com/ggerganov/llama.cpp) gguf files.

Expand Down
49 changes: 36 additions & 13 deletions test/hyllama.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe('ggufMetadata function', () => {
view.setUint8(2, 'U'.charCodeAt(0))
view.setUint8(3, 'F'.charCodeAt(0))
view.setUint32(4, 1, true) // version
view.setBigUint64(8, BigInt(0), true) // tensorCount
view.setBigUint64(16, BigInt(1), true) // metadataKVCount
view.setBigUint64(8, 0n, true) // tensorCount
view.setBigUint64(16, 1n, true) // metadataKVCount

// Mock key-value pair
// Key-value pair
const key = 'testKey'
const value = 123
// Write key
Expand All @@ -31,19 +31,9 @@ describe('ggufMetadata function', () => {

const { metadata } = ggufMetadata(buffer)

// Assertions
expect(metadata).toHaveProperty(key, value)
})

it('throws an error for non-GGUF file', () => {
// Create a mock ArrayBuffer for a non-GGUF file
const buffer = new ArrayBuffer(4)
const view = new DataView(buffer)
view.setUint8(0, 'A'.charCodeAt(0)) // Invalid magic number

expect(() => ggufMetadata(buffer)).toThrow('Not a valid GGUF file')
})

it('parses metadata and tensor info from remote file', async () => {
const buf = await (
await fetch(URL_LLAMA, {
Expand Down Expand Up @@ -108,4 +98,37 @@ describe('ggufMetadata function', () => {
type: 0,
})
})

it('throws an error for non-GGUF file', () => {
const buffer = new ArrayBuffer(4)
const view = new DataView(buffer)
view.setUint8(0, 'A'.charCodeAt(0)) // invalid magic number
expect(() => ggufMetadata(buffer)).toThrow('Not a valid GGUF file')
})

it('throws an error for invalid GGUF file', () => {
const buffer = new ArrayBuffer(47)
const view = new DataView(buffer)
view.setUint8(0, 'G'.charCodeAt(0))
view.setUint8(1, 'G'.charCodeAt(0))
view.setUint8(2, 'U'.charCodeAt(0))
view.setUint8(3, 'F'.charCodeAt(0))
view.setUint32(4, 2, true) // version
view.setBigUint64(8, 9007199254740992n, true) // tensorCount > MAX_SAFE_INTEGER
view.setBigUint64(16, 1n, true) // metadataKVCount

// Key-value pair
const key = 'testKey'
const value = 123
// Write key
view.setBigUint64(24, BigInt(key.length), true) // key length
for (let i = 0; i < key.length; i++) {
view.setUint8(32 + i, key.charCodeAt(i))
}
// Write value
view.setUint32(32 + key.length, 88, true) // value type (UINT32)
view.setUint32(36 + key.length, value, true) // value

expect(() => ggufMetadata(buffer)).toThrow('Unsupported metadata type: 88')
})
})

0 comments on commit 66bf169

Please sign in to comment.