Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

6.7 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
promise_test(async function () {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue("hello");
      controller.close();
    }
  });
  const reader = stream.getReader();
  const first = await reader.read();
  const second = await reader.read();

  assert_true(stream instanceof ReadableStream);
  assert_equals(first.value, "hello");
  assert_false(first.done);
  assert_equals(second.value, undefined);
  assert_true(second.done);
}, "ReadableStream getReader() reads queued chunks and close state");

test(function () {
  let desiredSize = null;
  const stream = new ReadableStream({
    start(controller) {
      desiredSize = controller.desiredSize;
    }
  }, { highWaterMark: 3 });

  assert_equals(desiredSize, 3);
  assert_true(stream instanceof ReadableStream);
  assert_throws_js(TypeError, function () {
    new ReadableStream("source");
  });
  assert_throws_js(TypeError, function () {
    new ReadableStream({}, "strategy");
  });
  assert_throws_js(TypeError, function () {
    new WritableStream("sink");
  });
  assert_throws_js(TypeError, function () {
    new TransformStream("transformer");
  });
  assert_throws_js(TypeError, function () {
    new TransformStream({}, {}, "strategy");
  });
}, "Stream constructors parse source, sink, transformer, and strategy arguments through WebIDL");

promise_test(async function () {
  let cancelReason = null;
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue("queued");
    },
    cancel(reason) {
      cancelReason = reason;
    }
  });

  await stream.cancel("stop");
  assert_equals(cancelReason, "stop");

  const result = await stream.getReader().read();
  assert_true(result.done);
  assert_equals(result.value, undefined);
}, "ReadableStream.cancel() clears queued chunks, closes the stream, and forwards the reason");

promise_test(async function (t) {
  const stream = new ReadableStream();

  assert_throws_js(TypeError, function () {
    stream.pipeThrough();
  });
  assert_throws_js(TypeError, function () {
    stream.pipeThrough(null);
  });
  await promise_rejects_js(t, TypeError, stream.pipeTo());
  await promise_rejects_js(t, TypeError, stream.pipeTo(null));
}, "ReadableStream pipe methods use their specified synchronous and promise rejection channels");

promise_test(async function () {
  const input = new ReadableStream({
    start(controller) {
      controller.enqueue("hello");
      controller.close();
    }
  });
  const upper = new TransformStream({
    transform(chunk, controller) {
      controller.enqueue(chunk.toUpperCase());
    }
  });

  const transformed = input.pipeThrough(upper);
  const result = await transformed.getReader().read();
  assert_true(upper instanceof TransformStream);
  assert_true(transformed instanceof ReadableStream);
  assert_equals(result.value, "HELLO");
  assert_false(result.done);
}, "ReadableStream.pipeThrough() pipes chunks through TransformStream");

promise_test(async function () {
  const defaultDesiredSizes = [];
  const defaultStream = new TransformStream({
    transform(chunk, controller) {
      defaultDesiredSizes.push(controller.desiredSize);
      controller.enqueue(chunk);
    }
  });
  const defaultWriter = defaultStream.writable.getWriter();
  const defaultReader = defaultStream.readable.getReader();
  const defaultRead = defaultReader.read();
  const defaultWrite = defaultWriter.write("x");
  const defaultResult = await defaultRead;
  await defaultWrite;
  await defaultWriter.close();
  assert_equals(defaultResult.value, "x");
  assert_false(defaultResult.done);
  assert_array_equals(defaultDesiredSizes, [0]);

  const explicitDesiredSizes = [];
  const explicitStream = new TransformStream({
    transform(chunk, controller) {
      explicitDesiredSizes.push(controller.desiredSize);
      controller.enqueue(chunk);
    }
  }, undefined, { highWaterMark: 4 });
  const explicitWriter = explicitStream.writable.getWriter();
  await explicitWriter.write("x");
  await explicitWriter.close();
  assert_array_equals(explicitDesiredSizes, [4]);
}, "TransformStream readable strategy defaults highWaterMark to 0 and honors explicit highWaterMark");

promise_test(async function () {
  const chunks = [];
  const output = new WritableStream({
    write(chunk) {
      chunks.push(chunk);
    }
  });
  const input = new ReadableStream({
    start(controller) {
      controller.enqueue("a");
      controller.enqueue("b");
      controller.close();
    }
  });

  assert_true(output instanceof WritableStream);
  await input.pipeTo(output);
  assert_array_equals(chunks, ["a", "b"]);
}, "ReadableStream.pipeTo() writes queued chunks to WritableStream");

promise_test(async function () {
  const encoder = new TextEncoderStream();
  const writer = encoder.writable.getWriter();
  const reader = encoder.readable.getReader();

  assert_equals(encoder.encoding, "utf-8");
  const read = reader.read();
  const write = writer.write("hi");
  const close = writer.close();
  const result = await read;
  await Promise.all([write, close]);
  assert_array_equals(Array.from(result.value), [104, 105]);
}, "TextEncoderStream encodes string chunks as UTF-8 bytes");

promise_test(async function () {
  const bytes = new Uint8Array([104, 101, 108, 108, 111]);
  const decoded = new ReadableStream({
    start(controller) {
      controller.enqueue(bytes);
      controller.close();
    }
  }).pipeThrough(new TextDecoderStream());

  const result = await decoded.getReader().read();
  assert_equals(result.value, "hello");
  assert_false(result.done);
}, "TextDecoderStream decodes UTF-8 byte chunks");

test(function () {
  const decoder = new TextDecoderStream("utf-8", null);
  assert_equals(decoder.encoding, "utf-8");
  assert_false(decoder.fatal);
  assert_false(decoder.ignoreBOM);

  const configured = new TextDecoderStream("utf-8", { fatal: 1, ignoreBOM: "yes" });
  assert_true(configured.fatal);
  assert_true(configured.ignoreBOM);

  const throwingLabel = {
    toString() {
      throw new RangeError("label conversion");
    }
  };
  const throwingOptions = {
    get fatal() {
      throw new RangeError("fatal conversion");
    }
  };

  assert_throws_js(TypeError, function () {
    new TextDecoderStream(Symbol("utf-8"));
  });
  assert_throws_js(RangeError, function () {
    new TextDecoderStream(throwingLabel);
  });
  assert_throws_js(TypeError, function () {
    new TextDecoderStream("utf-8", "options");
  });
  assert_throws_js(RangeError, function () {
    new TextDecoderStream("utf-8", throwingOptions);
  });
}, "TextDecoderStream constructor label and options follow WebIDL conversion");
</script>