packages/deriver/tests/Unit/Deriver/Execution/PartialObservationTest.php

1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Execution;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\BoundInstruction;
10use Deriver\Deriver\Evaluator\Call\Evaluators;
11use Deriver\Deriver\Evaluator\Call\Registrations;
12use Deriver\Deriver\Evaluator\EvaluatorConfig;
13use Deriver\Deriver\Evaluator\Evidence\Frontier;
14use Deriver\Deriver\Evaluator\Execution\Context;
15use Deriver\Deriver\Evaluator\Execution\EvaluatorConfig as ExecutionEvaluatorConfig;
16use Deriver\Deriver\Evaluator\Request\Budget;
17use Deriver\Deriver\Evaluator\Request\ReturnQuery;
18use Deriver\Deriver\Evaluator\TargetProfile;
19use Deriver\Deriver\Evaluator\Term;
20use Deriver\Deriver\Execution\PartialObservation;
21use Deriver\Deriver\Execution\QueryExecution;
22use Deriver\Deriver\Lowerer\Declaration\Index;
23use Deriver\Deriver\MicroEvaluatorConversion;
24use Deriver\Deriver\Parser\Node\File;
25use Deriver\Deriver\Parser\Parser;
26use Deriver\Deriver\Parser\ParserConfig;
27use Deriver\Deriver\Providers\ProviderExpansion;
28use Deriver\Deriver\Resolver\Resolver;
29use Deriver\Deriver\SnapshotIdentity;
30use Deriver\Deriver\Sources\Path;
31use Deriver\Deriver\Sources\Source;
32use Deriver\Deriver\Sources\SourceRef;
33use Deriver\Deriver\Sources\Sources;
34use PHPUnit\Framework\Attributes\CoversNothing;
35use PHPUnit\Framework\Attributes\Small;
36use PHPUnit\Framework\TestCase;
37
38#[CoversNothing]
39#[Small]
40final class PartialObservationTest extends TestCase
41{
42    public function testRecoverDoesNotInventMutableState(): void
43    {
44        $source = '<?php function target(){return 1;}';
45        $budget = new Budget();
46        $configuration = new DeriverConfig();
47        $contributions = new Contributions();
48        $input = new Sources([new Source('fixture.php', $source)]);
49        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
50        $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
51        $contributions1 = $providers->contributions;
52        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
53        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
54        $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
55        $snapshotId = $identity->id;
56        $input1 = $providers->input;
57        $parser = new Parser(new ParserConfig($target->version));
58        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
59        $program1 = new Index($snapshotId, $trees);
60        $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
61        $snapshotId1 = 'test';
62        $input2 = new Sources([new Source('fixture.php', $source)]);
63        $profile = $execution->configuration->target;
64        $parser1 = new Parser(new ParserConfig($profile->version));
65        $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(Path::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
66        $program = new Index($snapshotId1, $trees1);
67        $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
68        $partial = new PartialObservation($context->program, 'BUDGET_EXCEEDED');
69        self::assertNull($partial->recover(new ReturnQuery('target')));
70    }
71    public function testValueBoundsUnknownDependencies(): void
72    {
73        $source = '<?php function target(){return 1;}';
74        $budget = new Budget();
75        $configuration = new DeriverConfig();
76        $contributions = new Contributions();
77        $input = new Sources([new Source('fixture.php', $source)]);
78        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
79        $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
80        $contributions1 = $providers->contributions;
81        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
82        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
83        $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
84        $snapshotId = $identity->id;
85        $input1 = $providers->input;
86        $parser = new Parser(new ParserConfig($target->version));
87        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
88        $program1 = new Index($snapshotId, $trees);
89        $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
90        $snapshotId1 = 'test';
91        $input2 = new Sources([new Source('fixture.php', $source)]);
92        $profile = $execution->configuration->target;
93        $parser1 = new Parser(new ParserConfig($profile->version));
94        $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(Path::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
95        $program = new Index($snapshotId1, $trees1);
96        $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
97        $partial = new PartialObservation($context->program, 'BUDGET_EXCEEDED');
98        self::assertSame('BUDGET_EXCEEDED', $partial->value('absent')->literal);
99        self::assertFalse($partial->value('absent')->isConcrete());
100    }
101    public function testReadKeepsUnknownStorageUntyped(): void
102    {
103        $source = '<?php function target(){return 1;}';
104        $budget = new Budget();
105        $configuration = new DeriverConfig();
106        $contributions = new Contributions();
107        $input = new Sources([new Source('fixture.php', $source)]);
108        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
109        $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
110        $contributions1 = $providers->contributions;
111        $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
112        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
113        $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
114        $snapshotId = $identity->id;
115        $input1 = $providers->input;
116        $parser = new Parser(new ParserConfig($target->version));
117        $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
118        $program1 = new Index($snapshotId, $trees);
119        $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
120        $snapshotId1 = 'test';
121        $input2 = new Sources([new Source('fixture.php', $source)]);
122        $profile = $execution->configuration->target;
123        $parser1 = new Parser(new ParserConfig($profile->version));
124        $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(Path::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
125        $program = new Index($snapshotId1, $trees1);
126        $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
127        $partial = new PartialObservation($context->program, 'BUDGET_EXCEEDED');
128        $instruction = new BoundInstruction('read', 'read', new SourceRef('test', 'a.php', 0, 1), 'value', ['missing']);
129        self::assertSame('mixed', $partial->read($instruction)->attributes['type']);
130        self::assertSame('BUDGET_EXCEEDED', $partial->read($instruction)->literal);
131    }
132}
133