function assert(cond, msg) { if (!cond) throw new Error("Assertion failed: " + msg); }
(function testWeakRefBasic() {
let target = { name: "test" };
const wr = new WeakRef(target);
const derefed = wr.deref();
assert(derefed === target, "WeakRef.deref() should return target");
assert(derefed.name === "test", "deref().name should be 'test'");
console.log("PASS: WeakRef basic deref");
})();
(function testWeakRefSymbol() {
const sym = Symbol("test");
const wr = new WeakRef(sym);
const derefed = wr.deref();
assert(derefed === sym, "WeakRef.deref() should return symbol");
console.log("PASS: WeakRef symbol deref");
})();
(function testWeakRefValidation() {
try {
new WeakRef(42);
assert(false, "Should have thrown");
} catch (e) {
assert(e instanceof TypeError, "Should be TypeError for primitive target");
console.log("PASS: WeakRef rejects primitive target");
}
try {
new WeakRef("string");
assert(false, "Should have thrown");
} catch (e) {
assert(e instanceof TypeError, "Should be TypeError for string target");
console.log("PASS: WeakRef rejects string target");
}
try {
new WeakRef(Symbol.for("registered"));
assert(false, "Should have thrown");
} catch (e) {
assert(e instanceof TypeError, "Should be TypeError for registered symbol");
console.log("PASS: WeakRef rejects registered symbol");
}
})();
(function testWeakRefToStringTag() {
const wr = new WeakRef({});
const tag = Object.prototype.toString.call(wr);
assert(tag === "[object WeakRef]", "toStringTag should be WeakRef, got: " + tag);
console.log("PASS: WeakRef toStringTag");
})();
(function testWeakRefInstanceof() {
const wr = new WeakRef({});
assert(wr instanceof WeakRef, "should be instanceof WeakRef");
console.log("PASS: WeakRef instanceof");
})();
(function testWeakRefCtorProps() {
assert(WeakRef.length === 1, "WeakRef.length should be 1");
assert(WeakRef.name === "WeakRef", "WeakRef.name should be 'WeakRef'");
console.log("PASS: WeakRef constructor properties");
})();
console.log("All WeakRef tests passed!");