11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { describe, it } = require('node:test');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cidescribe('assert.CallTracker.getCalls()', { concurrency: true }, () => {
81cb0ef41Sopenharmony_ci  const tracker = new assert.CallTracker();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  it('should return empty list when no calls', () => {
111cb0ef41Sopenharmony_ci    const fn = tracker.calls();
121cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tracker.getCalls(fn), []);
131cb0ef41Sopenharmony_ci  });
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  it('should return calls', () => {
161cb0ef41Sopenharmony_ci    const fn = tracker.calls(() => {});
171cb0ef41Sopenharmony_ci    const arg1 = {};
181cb0ef41Sopenharmony_ci    const arg2 = {};
191cb0ef41Sopenharmony_ci    fn(arg1, arg2);
201cb0ef41Sopenharmony_ci    fn.call(arg2, arg2);
211cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tracker.getCalls(fn), [
221cb0ef41Sopenharmony_ci      { arguments: [arg1, arg2], thisArg: undefined },
231cb0ef41Sopenharmony_ci      { arguments: [arg2], thisArg: arg2 }]);
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  it('should throw when getting calls of a non-tracked function', () => {
271cb0ef41Sopenharmony_ci    [() => {}, 1, true, null, undefined, {}, []].forEach((fn) => {
281cb0ef41Sopenharmony_ci      assert.throws(() => tracker.getCalls(fn), { code: 'ERR_INVALID_ARG_VALUE' });
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  it('should return a frozen object', () => {
331cb0ef41Sopenharmony_ci    const fn = tracker.calls();
341cb0ef41Sopenharmony_ci    fn();
351cb0ef41Sopenharmony_ci    const calls = tracker.getCalls(fn);
361cb0ef41Sopenharmony_ci    assert.throws(() => calls.push(1), /object is not extensible/);
371cb0ef41Sopenharmony_ci    assert.throws(() => Object.assign(calls[0], { foo: 'bar' }), /object is not extensible/);
381cb0ef41Sopenharmony_ci    assert.throws(() => calls[0].arguments.push(1), /object is not extensible/);
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cidescribe('assert.CallTracker.reset()', () => {
431cb0ef41Sopenharmony_ci  const tracker = new assert.CallTracker();
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  it('should reset calls', () => {
461cb0ef41Sopenharmony_ci    const fn = tracker.calls();
471cb0ef41Sopenharmony_ci    fn();
481cb0ef41Sopenharmony_ci    fn();
491cb0ef41Sopenharmony_ci    fn();
501cb0ef41Sopenharmony_ci    assert.strictEqual(tracker.getCalls(fn).length, 3);
511cb0ef41Sopenharmony_ci    tracker.reset(fn);
521cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tracker.getCalls(fn), []);
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  it('should reset all calls', () => {
561cb0ef41Sopenharmony_ci    const fn1 = tracker.calls();
571cb0ef41Sopenharmony_ci    const fn2 = tracker.calls();
581cb0ef41Sopenharmony_ci    fn1();
591cb0ef41Sopenharmony_ci    fn2();
601cb0ef41Sopenharmony_ci    assert.strictEqual(tracker.getCalls(fn1).length, 1);
611cb0ef41Sopenharmony_ci    assert.strictEqual(tracker.getCalls(fn2).length, 1);
621cb0ef41Sopenharmony_ci    tracker.reset();
631cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tracker.getCalls(fn1), []);
641cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tracker.getCalls(fn2), []);
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  it('should throw when resetting a non-tracked function', () => {
691cb0ef41Sopenharmony_ci    [() => {}, 1, true, null, {}, []].forEach((fn) => {
701cb0ef41Sopenharmony_ci      assert.throws(() => tracker.reset(fn), { code: 'ERR_INVALID_ARG_VALUE' });
711cb0ef41Sopenharmony_ci    });
721cb0ef41Sopenharmony_ci  });
731cb0ef41Sopenharmony_ci});
74