Files
SnowLuma/packages/core/tests/hex.test.ts
T
手瓜一十雪 39f1672b29 Migrate project to pnpm monorepo
Restructure the repository into a pnpm workspace under packages/ (core, webui, runtime). Move source files for core and webui into packages/*, add package manifests for @snowluma/core and @snowluma/runtime, and remove the old launcher package.json. Update root package.json to be a monorepo controller (scripts delegate to pnpm filters, set packageManager and Node engine). Configure build outputs to emit into the repo-root dist/ (vite config changes) so the existing release pipeline continues to work. Add .npmrc with mirrors, expand .gitignore for monorepo layout, add tsconfig base and package-level tsconfigs, and add vitest config for the core package. Adjust injector native search paths and update tests import paths to the new package layout. Small CI/workflow comment added to clarify build behavior and README updated with monorepo structure and common commands.
2026-05-03 14:02:29 +08:00

66 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { toHex, fromHex, hexPreview } from '../src/utils/hex';
describe('toHex', () => {
it('converts empty buffer', () => {
expect(toHex(new Uint8Array(0))).toBe('');
});
it('converts single byte', () => {
expect(toHex(new Uint8Array([0xff]))).toBe('ff');
expect(toHex(new Uint8Array([0x00]))).toBe('00');
expect(toHex(new Uint8Array([0x0a]))).toBe('0a');
});
it('converts multiple bytes', () => {
expect(toHex(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))).toBe('deadbeef');
});
it('works with Buffer', () => {
expect(toHex(Buffer.from([0x01, 0x23, 0x45]))).toBe('012345');
});
});
describe('fromHex', () => {
it('converts empty string', () => {
expect(fromHex('')).toEqual(Buffer.alloc(0));
});
it('converts hex string', () => {
const buf = fromHex('deadbeef');
expect(buf).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef]));
});
it('handles uppercase', () => {
const buf = fromHex('DEADBEEF');
expect(buf).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef]));
});
it('throws on odd-length string', () => {
expect(() => fromHex('abc')).toThrow('Invalid hex string length');
});
it('roundtrips with toHex', () => {
const original = Buffer.from([0x00, 0x7f, 0x80, 0xff]);
expect(fromHex(toHex(original))).toEqual(original);
});
});
describe('hexPreview', () => {
it('shows full hex for small data', () => {
const data = new Uint8Array([0xab, 0xcd]);
expect(hexPreview(data)).toBe('abcd');
});
it('truncates with ellipsis', () => {
const data = new Uint8Array(128).fill(0xaa);
const preview = hexPreview(data, 4);
expect(preview).toBe('aaaaaaaa...');
});
it('no ellipsis when exactly at limit', () => {
const data = new Uint8Array(4).fill(0xbb);
expect(hexPreview(data, 4)).toBe('bbbbbbbb');
});
});