hyparquet/test/asyncbuffer.test.js
Sylvain Lesage 09ae9400c5
build types before publishing to npm (#46)
* build types before publishing to npm

* use prepare instead of prepublishOnly + make it clear that we only build types

doc for prepare vs prepublishOnly is here: https://docs.npmjs.com/cli/v8/using-npm/scripts

* no jsx in this lib

* relative imports from the root, so that it works from types/

* remove unused hyparquet.d.ts + report differences to jsdoc in files

* try to understand if this is the cause of the failing CI check

tsc fails: https://github.com/hyparam/hyparquet/actions/runs/12040954822/job/33571851170?pr=46

* Revert "try to understand if this is the cause of the failing CI check"

This reverts commit 5e2fc8ca179064369de71793ab1cda3facefddc7.

* not sure what happens, but we just need to ensure the types are created correctly

* increment version

* Explicitly export types for use in downstream typescript projects

* Use new typescript jsdoc imports for smaller package

* Combine some files and use @import jsdoc

* use the local typescript

---------

Co-authored-by: Kenny Daniel <platypii@gmail.com>
2024-12-02 17:47:42 +01:00

45 lines
1.6 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest'
import { cachedAsyncBuffer } from '../src/utils.js'
describe('cachedAsyncBuffer', () => {
it('caches slices of a file to avoid multiple reads', async () => {
const slice = vi.fn(async (start, end) => {
// Simulate an async slice operation
await new Promise(resolve => setTimeout(resolve, 10))
if (end === undefined) end = 1000
if (start < 0) start = Math.max(0, 1000 + start)
const buffer = new ArrayBuffer(end - start)
return buffer
})
const cachedFile = cachedAsyncBuffer({
byteLength: 1000,
slice,
})
// Test cache miss
const slice1 = await cachedFile.slice(0, 100)
expect(slice).toHaveBeenCalledTimes(1)
expect(slice1.byteLength).toBe(100)
// Test cache hit for the same range
const slice2 = await cachedFile.slice(0, 100)
expect(slice).toHaveBeenCalledTimes(1) // No additional call
expect(slice2).toBe(slice1) // Exact same object from cache
// Test cache with undefined end, should use byteLength as end
const slice3 = await cachedFile.slice(900)
expect(slice).toHaveBeenCalledTimes(2)
expect(slice3.byteLength).toBe(100)
// Test cache hit for suffix-range
const slice4 = await cachedFile.slice(-100)
expect(slice).toHaveBeenCalledTimes(2) // Still no additional call
expect(slice4).toBe(slice3) // Cached result reused
// Verify that asking for the same end implicitly gets from cache
const slice5 = await cachedFile.slice(900, 1000)
expect(slice).toHaveBeenCalledTimes(2) // Still no additional call
expect(slice5).toBe(slice3) // Cached result reused
})
})