/ test / search-narrowing.spec.js
search-narrowing.spec.js
 1  import { fact, Inspector } from './lib.js'
 2  import db, { arnold } from './movie.db.js'
 3  
 4  /**
 5   * @type {import('entail').Suite}
 6   */
 7  export const testPlan = {
 8    'test that search space is getting reduced': async (assert) => {
 9      const source = Inspector.from(db)
10      const Movie = fact({
11        the: 'movie',
12        title: String,
13        cast: Object,
14      })
15  
16      const Person = fact({
17        the: 'person',
18        name: String,
19      })
20  
21      const MoviesCastingArnold = Movie.with({ cast: Object }).where(
22        ({ this: movie, title, cast }) => [
23          Movie({ this: movie, title, cast }),
24          Person({ this: cast, name: 'Arnold Schwarzenegger' }),
25        ]
26      )
27  
28      const result = await MoviesCastingArnold.match().query({ from: source })
29  
30      assert.deepEqual(result.length, 5)
31  
32      assert.deepEqual(
33        source.queries().slice(0, 2),
34        [
35          { the: 'person/name', is: 'Arnold Schwarzenegger' },
36          { the: 'movie/cast', is: arnold },
37        ],
38        'narrows search space first'
39      )
40  
41      assert.equal(
42        source
43          .queries()
44          .slice(2)
45          .every(
46            (selector) =>
47              selector.the === 'movie/title' &&
48              selector.of != null &&
49              selector.is == null
50          ),
51        true,
52        'rest queries are just lookups'
53      )
54    },
55  }