Skip to content

Lightpanda: the headless browser designed for AI and automation

add unit tests for dump

Karl Seguinbedde28ddef1
diff --git a/src/browser/dump.zig b/src/browser/dump.zig
index 375952ca7..ca7736211 100644
--- a/src/browser/dump.zig
+++ b/src/browser/dump.zig
@@ -368,3 +368,60 @@ fn writeEscapedByte(input: []const u8, index: usize, writer: *std.Io.Writer) ![]
}
return input[index + 1 ..];
}
+
+const testing = @import("../testing.zig");
+
+// A fresh page per assertion: `with_base` mutates the document (it inserts a
+// <base> element), so reusing one frame across opts would leak that mutation
+// into later dumps.
+fn expectDump(opts: Opts, expected: []const u8) !void {
+ var frame = try testing.pageTest("dump.html", .{});
+ defer testing.reset();
+ defer frame._session.removePage();
+
+ var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
+ try root(frame.window._document, opts, &aw.writer, frame);
+ try testing.expectString(expected, aw.written());
+}
+
+test "dump: default dumps the whole document" {
+ try expectDump(.{},
+ \\<!DOCTYPE html>
+ \\<html><head><style>.hidden{display:none}</style><link rel="stylesheet" href="data:text/css,"><script>var a=1;</script></head><body><h1>Title</h1><p class="hidden">secret</p><img><svg></svg><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
+ );
+}
+
+test "dump: with_base injects a <base> element" {
+ try expectDump(.{ .with_base = true },
+ \\<!DOCTYPE html>
+ \\<html><head><base base="http://127.0.0.1:9582/src/browser/tests/dump.html"></base><style>.hidden{display:none}</style><link rel="stylesheet" href="data:text/css,"><script>var a=1;</script></head><body><h1>Title</h1><p class="hidden">secret</p><img><svg></svg><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
+ );
+}
+
+test "dump: strip.js removes script and noscript" {
+ try expectDump(.{ .strip = .{ .js = true } },
+ \\<!DOCTYPE html>
+ \\<html><head><style>.hidden{display:none}</style><link rel="stylesheet" href="data:text/css,"></head><body><h1>Title</h1><p class="hidden">secret</p><img><svg></svg><p>visible &amp; well</p></body></html>
+ );
+}
+
+test "dump: strip.css removes style and stylesheet links" {
+ try expectDump(.{ .strip = .{ .css = true } },
+ \\<!DOCTYPE html>
+ \\<html><head><script>var a=1;</script></head><body><h1>Title</h1><p class="hidden">secret</p><img><svg></svg><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
+ );
+}
+
+test "dump: strip.ui removes css plus visual elements" {
+ try expectDump(.{ .strip = .{ .ui = true } },
+ \\<!DOCTYPE html>
+ \\<html><head><script>var a=1;</script></head><body><h1>Title</h1><p class="hidden">secret</p><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
+ );
+}
+
+test "dump: strip.invisible removes author display:none elements" {
+ try expectDump(.{ .strip = .{ .invisible = true } },
+ \\<!DOCTYPE html>
+ \\<html><head><style>.hidden{display:none}</style><link rel="stylesheet" href="data:text/css,"><script>var a=1;</script></head><body><h1>Title</h1><img><svg></svg><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
+ );
+}
diff --git a/src/browser/tests/dump.html b/src/browser/tests/dump.html
new file mode 100644
index 000000000..462a2e1f1
--- /dev/null
+++ b/src/browser/tests/dump.html
@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><style>.hidden{display:none}</style><link rel="stylesheet" href="data:text/css,"><script>var a=1;</script></head><body><h1>Title</h1><p class="hidden">secret</p><img><svg></svg><noscript>nojs</noscript><p>visible &amp; well</p></body></html>
\ No newline at end of file