constraint.spec.js
1 import { Memory, $, fact, Collection, Text } from './lib.js' 2 3 const db = Memory.create({ 4 import: { 5 'content/words': ['pizza', 'store/*', 'store/add', '*', '[a-z]'], 6 }, 7 }) 8 9 const Content = fact({ the: 'content', words: Object }) 10 11 /** 12 * @type {import('entail').Suite} 13 */ 14 export const testConstraints = { 15 'test like': async (assert) => { 16 const Piz = fact({ word: String }) 17 .with({ source: Object, words: Object }) 18 .where(({ word, words, _ }) => [ 19 Content({ words }), 20 Collection({ this: words, of: word }), 21 Text.match({ 22 this: word, 23 pattern: 'piz*a', 24 }), 25 Piz.claim({ word }), 26 ]) 27 28 assert.deepEqual(await Piz().query({ from: db }), [ 29 Piz.assert({ word: 'pizza' }), 30 ]) 31 }, 32 33 'make pattern rule': async (assert) => { 34 const Like = fact({ word: String, like: String }) 35 .with({ words: Object }) 36 .where(({ word, words, like }) => [ 37 Content({ words }), 38 Collection({ this: words, of: word }), 39 Text.match({ 40 this: word, 41 pattern: like, 42 }), 43 Like.claim({ word, like }), 44 ]) 45 46 assert.deepEqual( 47 await Like({ like: 'piz*', word: $ }).query({ from: db }), 48 [Like.assert({ word: 'pizza', like: 'piz*' })] 49 ) 50 51 assert.deepEqual(await Like.match({ like: 'piz%' }).query({ from: db }), []) 52 53 assert.deepEqual(await Like.match({ like: 'Piz*' }).query({ from: db }), []) 54 55 assert.deepEqual( 56 await Like.match({ like: 'piz\\*' }).query({ from: db }), 57 [] 58 ) 59 assert.deepEqual( 60 await Like({ like: 'piz?a', word: $ }).query({ from: db }), 61 [Like.assert({ word: 'pizza', like: 'piz?a' })] 62 ) 63 64 assert.deepEqual( 65 await Like({ like: 'store/*', word: $ }).query({ from: db }), 66 [ 67 Like.assert({ word: 'store/*', like: 'store/*' }), 68 Like.assert({ word: 'store/add', like: 'store/*' }), 69 ] 70 ) 71 72 assert.deepEqual(await Like({ like: '*', word: $ }).query({ from: db }), [ 73 Like.assert({ word: 'pizza', like: '*' }), 74 Like.assert({ word: 'store/*', like: '*' }), 75 Like.assert({ word: 'store/add', like: '*' }), 76 Like.assert({ word: '*', like: '*' }), 77 Like.assert({ word: '[a-z]', like: '*' }), 78 ]) 79 }, 80 81 'test find patterns that match text': async (assert) => { 82 const Pattern = fact({ pattern: String }) 83 .with({ words: Object }) 84 .where(({ pattern, words }) => [ 85 Content({ words }), 86 Collection({ this: words, of: pattern }), 87 Text.match({ 88 this: 'store/list', 89 pattern, 90 }), 91 Pattern.claim({ pattern }), 92 ]) 93 94 assert.deepEqual(await Pattern().query({ from: db }), [ 95 Pattern.assert({ pattern: 'store/*' }), 96 Pattern.assert({ pattern: '*' }), 97 ]) 98 }, 99 100 'test revers pattern': async (assert) => { 101 const Match = fact({ word: String }) 102 .with({ words: Object }) 103 .where(({ word, words }) => [ 104 Content({ words }), 105 Collection({ this: words, of: word }), 106 Text.match({ 107 this: word, 108 pattern: '\\*', 109 }), 110 Match.claim({ word }), 111 ]) 112 113 assert.deepEqual(await Match().query({ from: db }), [ 114 Match.assert({ word: '*' }), 115 ]) 116 }, 117 }