frames.spec.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { describe, expect, it } from 'vitest'
  2. import { parseInboundFrame } from '../../src/transport/frames.ts'
  3. describe('tunnel init frame', () => {
  4. it('retains the selected overlay order', () => {
  5. expect(parseInboundFrame({
  6. t: 'init',
  7. image: 'base.tar.gz',
  8. overlays: ['workspace.tar.gz', 'session.tar.gz'],
  9. })).toEqual({
  10. t: 'init',
  11. image: 'base.tar.gz',
  12. overlays: ['workspace.tar.gz', 'session.tar.gz'],
  13. })
  14. })
  15. it('rejects a missing or non-string overlay list', () => {
  16. expect(() => parseInboundFrame({ t: 'init', image: 'base.tar.gz' })).toThrow(/array of string overlay urls/)
  17. expect(() => parseInboundFrame({ t: 'init', image: 'base.tar.gz', overlays: [1] }))
  18. .toThrow(/array of string overlay urls/)
  19. })
  20. })
  21. describe('tunnel request bodies', () => {
  22. it('accepts ArrayBuffer, Blob, and ReadableStream bodies and rejects other values', () => {
  23. const bytes = Uint8Array.of(1, 2).buffer
  24. const blob = new Blob(['large'])
  25. expect(parseInboundFrame({
  26. t: 'req', id: 1, method: 'POST', url: '/bytes', headers: {}, body: bytes,
  27. })).toMatchObject({ body: bytes })
  28. expect(parseInboundFrame({
  29. t: 'req', id: 2, method: 'POST', url: '/blob', headers: {}, body: blob,
  30. })).toMatchObject({ body: blob })
  31. const stream = new ReadableStream<Uint8Array>()
  32. expect(parseInboundFrame({
  33. t: 'req', id: 3, method: 'POST', url: '/stream', headers: {}, body: stream,
  34. })).toMatchObject({ body: stream })
  35. expect(() => parseInboundFrame({
  36. t: 'req', id: 4, method: 'POST', url: '/bad', headers: {}, body: 'large',
  37. })).toThrow('body must be an ArrayBuffer, Blob, or ReadableStream')
  38. })
  39. })