CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
createProxy.test.ts165 linesDownload Raw Back to utils
1import { vi, it, expect } from 'vitest'2import { createProxy } from './createProxy'3 4it('does not interfere with default constructors', () => {5  const ProxyClass = createProxy(6    class {7      constructor(public name: string) {}8    },9    {}10  )11 12  const instance = new ProxyClass('John')13  expect(instance.name).toBe('John')14})15 16it('does not interfere with default getters', () => {17  const proxy = createProxy({ foo: 'initial' }, {})18  expect(proxy.foo).toBe('initial')19})20 21it('does not interfere with default setters', () => {22  const proxy = createProxy({ foo: 'initial' }, {})23  proxy.foo = 'next'24 25  expect(proxy.foo).toBe('next')26})27 28it('does not interfere with default methods', () => {29  const proxy = createProxy({ getValue: () => 'initial' }, {})30  expect(proxy.getValue()).toBe('initial')31})32 33it('does not interfere with existing descriptors', () => {34  const target = {} as { foo: string; bar: number }35  let internalBar = 036 37  Object.defineProperties(target, {38    foo: {39      get: () => 'initial',40    },41    bar: {42      set: (value) => {43        internalBar = value + 1044      },45    },46  })47 48  const proxy = createProxy(target, {49    getProperty(data, next) {50      return next()51    },52  })53  expect(proxy.foo).toBe('initial')54 55  proxy.bar = 556  expect(proxy.bar).toBeUndefined()57  expect(internalBar).toBe(15)58})59 60it('infer prototype descriptors', () => {61  class Child {62    ok: boolean63 64    set status(nextStatus: number) {65      this.ok = nextStatus >= 200 && nextStatus < 30066    }67  }68 69  Object.defineProperties(Child.prototype, {70    status: { enumerable: true },71  })72 73  const scope = {} as { child: typeof Child }74 75  Object.defineProperty(scope, 'child', {76    enumerable: true,77    value: Child,78  })79 80  const ProxyClass = createProxy(scope.child, {})81  const instance = new ProxyClass()82 83  instance.status = 20184  expect(instance.ok).toBe(true)85})86 87it('spies on the constructor', () => {88  const OriginalClass = class {89    constructor(public name: string, public age: number) {}90  }91 92  const constructorCall = vi.fn<93    (94      args: ConstructorParameters<typeof OriginalClass>,95      next: () => typeof OriginalClass96    ) => typeof OriginalClass97  >((args, next) => next())98 99  const ProxyClass = createProxy(OriginalClass, {100    constructorCall,101  })102 103  new ProxyClass('John', 32)104 105  expect(constructorCall).toHaveBeenCalledTimes(1)106  expect(constructorCall).toHaveBeenCalledWith(107    ['John', 32],108    expect.any(Function)109  )110})111 112it('spies on property getters', () => {113  const getProperty = vi.fn((args, next) => next())114  const proxy = createProxy({ foo: 'initial' }, { getProperty })115 116  proxy.foo117 118  expect(getProperty).toHaveBeenCalledTimes(1)119  expect(getProperty).toHaveBeenCalledWith(['foo', proxy], expect.any(Function))120})121 122it('spies on property setters', () => {123  const setProperty = vi.fn((args, next) => next())124  const proxy = createProxy({ foo: 'initial' }, { setProperty })125 126  proxy.foo = 'next'127 128  expect(setProperty).toHaveBeenCalledTimes(1)129  expect(setProperty).toHaveBeenCalledWith(130    ['foo', 'next'],131    expect.any(Function)132  )133})134 135it('spies on method calls', () => {136  const methodCall = vi.fn((args, next) => next())137  const proxy = createProxy(138    {139      greet: (name: string) => `hello ${name}`,140    },141    { methodCall }142  )143 144  proxy.greet('Clair')145 146  expect(methodCall).toHaveBeenCalledTimes(1)147  expect(methodCall).toHaveBeenCalledWith(148    ['greet', ['Clair']],149    expect.any(Function)150  )151})152 153it('proxies properties on the prototype level', () => {154  const method = vi.fn()155  const prototype = { method }156 157  const proxy = createProxy(Object.create(prototype), {})158  const proxyMethod = vi.fn()159  proxy.method = proxyMethod160 161  prototype.method()162  expect(method).toHaveBeenCalledTimes(0)163  expect(proxyMethod).toHaveBeenCalledTimes(1)164})165 
basant307/AI_Governance_Project · CoolFace