a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore(lexicons): test with and without eval

Mary 8a374739 db2ae8cf

+90 -59
+90 -59
packages/lexicons/lexicons/lib/validations/index.test.ts
··· 1 - import { expect, it } from 'vitest'; 1 + import { describe, expect, it, vi } from 'vitest'; 2 2 3 3 import * as v from './index.js'; 4 + import { allowsEval } from './utils.js'; 4 5 5 - it('performs validation', () => { 6 - const subobjectSchema = v.object({ 7 - $type: v.optional(v.literal('com.example.kitchenSink#subobject')), 8 - boolean: v.boolean(), 9 - }); 6 + describe.each([[false], [true]])(`with eval: %p`, (withEval) => { 7 + it('performs validation', () => { 8 + using _mock = vi.spyOn(allowsEval, 'value', 'get').mockReturnValue(withEval); 10 9 11 - const objectSchema = v.object({ 12 - $type: v.optional(v.literal('com.example.kitchenSink#object')), 13 - get object() { 14 - return subobjectSchema; 15 - }, 16 - array: v.array(v.string()), 17 - boolean: v.boolean(), 18 - integer: v.integer(), 19 - integerWithDefault: v.optional(v.integer(), 42), 20 - integerWithDefaultFn: v.optional(v.integer(), () => 421), 21 - string: v.string(), 22 - }); 10 + const subobjectSchema = v.object({ 11 + $type: v.optional(v.literal('com.example.kitchenSink#subobject')), 12 + boolean: v.boolean(), 13 + }); 23 14 24 - const recordSchema = v.record( 25 - v.tidString(), 26 - v.object({ 27 - $type: v.literal('com.example.kitchenSink'), 15 + const objectSchema = v.object({ 16 + $type: v.optional(v.literal('com.example.kitchenSink#object')), 28 17 get object() { 29 - return objectSchema; 18 + return subobjectSchema; 30 19 }, 31 20 array: v.array(v.string()), 32 21 boolean: v.boolean(), 33 22 integer: v.integer(), 23 + integerFilled: v.optional(v.integer()), 24 + integerOptional: v.optional(v.integer()), 25 + integerWithDefault: v.optional(v.integer(), 42), 26 + integerWithDefaultFn: v.optional(v.integer(), () => 421), 34 27 string: v.string(), 28 + }); 29 + 30 + const recordSchema = v.record( 31 + v.tidString(), 32 + v.object({ 33 + $type: v.literal('com.example.kitchenSink'), 34 + get object() { 35 + return objectSchema; 36 + }, 37 + array: v.array(v.string()), 38 + boolean: v.boolean(), 39 + integer: v.integer(), 40 + string: v.string(), 41 + 42 + atUri: v.resourceUriString(), 43 + datetime: v.datetimeString(), 44 + did: v.didString(), 45 + cid: v.cidString(), 35 46 36 - atUri: v.resourceUriString(), 37 - datetime: v.datetimeString(), 38 - did: v.didString(), 39 - cid: v.cidString(), 47 + bytes: v.bytes(), 48 + cidLink: v.cidLink(), 49 + }), 50 + ); 40 51 41 - bytes: v.bytes(), 42 - cidLink: v.cidLink(), 43 - }), 44 - ); 52 + const datetime = new Date().toISOString(); 45 53 46 - const res: v.InferInput<typeof recordSchema> = { 47 - $type: 'com.example.kitchenSink', 48 - object: { 49 - object: { boolean: true }, 54 + const res: v.InferInput<typeof recordSchema> = { 55 + $type: 'com.example.kitchenSink', 56 + object: { 57 + object: { boolean: true }, 58 + array: ['one', 'two'], 59 + boolean: true, 60 + integer: 123, 61 + integerFilled: 234, 62 + string: 'string', 63 + }, 50 64 array: ['one', 'two'], 51 65 boolean: true, 52 66 integer: 123, 53 67 string: 'string', 54 - }, 55 - array: ['one', 'two'], 56 - boolean: true, 57 - integer: 123, 58 - string: 'string', 59 - datetime: new Date().toISOString(), 60 - atUri: 'at://did:web:example.com/com.example.test/self', 61 - did: 'did:web:example.com', 62 - cid: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 63 - bytes: { 64 - $bytes: 'AAECAw', 65 - }, 66 - cidLink: { 67 - $link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 68 - }, 69 - }; 70 - 71 - v.parse(recordSchema, res); 72 - }); 68 + datetime: datetime, 69 + atUri: 'at://did:web:example.com/com.example.test/self', 70 + did: 'did:web:example.com', 71 + cid: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 72 + bytes: { 73 + $bytes: 'AAECAw', 74 + }, 75 + cidLink: { 76 + $link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 77 + }, 78 + }; 73 79 74 - it('sets optional defaults', () => { 75 - const objectSchema = v.object({ 76 - foo: v.optional(v.integer(), 123), 80 + const result = v.parse(recordSchema, res); 81 + expect(result).toEqual({ 82 + $type: 'com.example.kitchenSink', 83 + array: ['one', 'two'], 84 + atUri: 'at://did:web:example.com/com.example.test/self', 85 + boolean: true, 86 + bytes: { 87 + $bytes: 'AAECAw', 88 + }, 89 + cid: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 90 + cidLink: { 91 + $link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 92 + }, 93 + datetime: datetime, 94 + did: 'did:web:example.com', 95 + integer: 123, 96 + object: { 97 + array: ['one', 'two'], 98 + boolean: true, 99 + integer: 123, 100 + integerFilled: 234, 101 + integerWithDefault: 42, 102 + integerWithDefaultFn: 421, 103 + object: { 104 + boolean: true, 105 + }, 106 + string: 'string', 107 + }, 108 + string: 'string', 109 + }); 77 110 }); 78 - 79 - expect(v.parse(objectSchema, {})).toEqual({ foo: 123 }); 80 111 });