current commit
260727.8 · test: ensure assertions are reached on all tests
// Flags: --experimental-quic --experimental-stream-iter --no-warnings

// Test: basic unidirectional stream data transfer.
// The client creates a unidirectional stream with a body. The server reads
// the data and verifies integrity. The unidirectional stream is write-only
// on the client side and read-only on the server side.

import { hasQuic, skip, mustCall } from '../common/index.mjs';
import * as assert from 'node:assert';

if (!hasQuic) {
  skip('QUIC is not enabled');
}

const { listen, connect } = await import('../common/quic.mjs');
const { bytes } = await import('stream/iter');

const message = 'unidirectional payload';
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const body = encoder.encode(message);
const expected = encoder.encode(message);

const done = Promise.withResolvers();

const serverEndpoint = await listen(mustCall((serverSession) => {
  serverSession.onstream = mustCall(async (stream) => {
    assert.strictEqual(stream.direction, 'uni');

    const received = await bytes(stream);
    assert.deepStrictEqual(received, expected);
    assert.strictEqual(decoder.decode(received), message);

    // The server side of a remote unidirectional stream is not writable.
    // The writer should be pre-closed (canWrite returns null).
    const w = stream.writer;
    assert.strictEqual(w.canWrite, null);
    assert.strictEqual(w.endSync(), 0);

    await stream.closed;
    serverSession.close();
    done.resolve();
  });
}));

const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

const stream = await clientSession.createUnidirectionalStream({ body });
assert.strictEqual(stream.direction, 'uni');

// The client-side uni stream is write-only — async iteration yields nothing.
const iter = stream[Symbol.asyncIterator]();
const { done: iterDone } = await iter.next();
assert.strictEqual(iterDone, true);

await done.promise;
// The server closed its session, delivering CONNECTION_CLOSE to the client.
// The client session enters the draining period, after which all streams
// and the session itself close cleanly.
await stream.closed;
await clientSession.close();
await serverEndpoint.close();