1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
pub mod curve;

use crate::conductor::api::CellConductorReadHandle;
use crate::conductor::api::MockCellConductorReadHandleT;
use crate::conductor::interface::SignalBroadcaster;
use crate::core::ribosome::guest_callback::entry_defs::EntryDefsHostAccess;
use crate::core::ribosome::guest_callback::entry_defs::EntryDefsInvocation;
use crate::core::ribosome::guest_callback::init::InitHostAccess;
use crate::core::ribosome::guest_callback::init::InitInvocation;
use crate::core::ribosome::guest_callback::migrate_agent::MigrateAgentHostAccess;
use crate::core::ribosome::guest_callback::migrate_agent::MigrateAgentInvocation;
use crate::core::ribosome::guest_callback::post_commit::PostCommitHostAccess;
use crate::core::ribosome::guest_callback::post_commit::PostCommitInvocation;
use crate::core::ribosome::guest_callback::validate::ValidateHostAccess;
use crate::core::ribosome::real_ribosome::RealRibosome;
use crate::core::ribosome::CallContext;
use crate::core::ribosome::FnComponents;
use crate::core::ribosome::HostContext;
use crate::core::ribosome::InvocationAuth;
use crate::core::ribosome::ZomeCallHostAccess;
use crate::core::ribosome::ZomeCallInvocation;
use crate::core::ribosome::ZomesToInvoke;
use crate::sweettest::SweetDnaFile;
use crate::test_utils::fake_genesis;
use ::fixt::prelude::*;
pub use holo_hash::fixt::*;
use holo_hash::WasmHash;
use holochain_keystore::MetaLairClient;
use holochain_p2p::HolochainP2pDnaFixturator;
use holochain_state::host_fn_workspace::HostFnWorkspace;
use holochain_state::host_fn_workspace::HostFnWorkspaceRead;
use holochain_state::test_utils::test_keystore;
use holochain_types::db_cache::DhtDbQueryCache;
use holochain_types::prelude::*;
use holochain_wasm_test_utils::TestWasm;
use rand::seq::IteratorRandom;
use rand::thread_rng;
use rand::Rng;
use std::collections::BTreeMap;
use std::sync::Arc;
use strum::IntoEnumIterator;

pub use holochain_types::fixt::*;

newtype_fixturator!(FnComponents<Vec<String>>);

fixturator!(
    RealRibosome;
    constructor fn empty(DnaFile);
);

impl Iterator for RealRibosomeFixturator<curve::Zomes> {
    type Item = RealRibosome;

    fn next(&mut self) -> Option<Self::Item> {
        let input = self.0.curve.0.clone();
        let uuid = StringFixturator::new(Unpredictable).next().unwrap();
        let (dna_file, _, _) = tokio_helper::block_forever_on(async move {
            SweetDnaFile::from_test_wasms(uuid, input, Default::default()).await
        });

        let ribosome = RealRibosome::new(dna_file).unwrap();

        // warm the module cache for each wasm in the ribosome
        for zome in self.0.curve.0.clone() {
            let mut call_context = CallContextFixturator::new(Empty).next().unwrap();
            call_context.zome = CoordinatorZome::from(zome).erase_type();
            ribosome.module(call_context.zome.zome_name()).unwrap();
        }

        self.0.index += 1;

        Some(ribosome)
    }
}

fixturator!(
    DnaWasm;
    // note that an empty wasm will not compile
    curve Empty DnaWasm { code: Default::default() };
    curve Unpredictable TestWasm::iter().choose(&mut thread_rng()).unwrap().into();
    curve Predictable TestWasm::iter().cycle().nth(get_fixt_index!()).unwrap().into();
);

fixturator!(
    WasmMap;
    curve Empty BTreeMap::new().into();
    curve Unpredictable {
        let mut rng = rand::thread_rng();
        let number_of_wasms = rng.gen_range(0..5);

        let mut wasms = BTreeMap::new();
        let mut dna_wasm_fixturator = DnaWasmFixturator::new(Unpredictable);
        for _ in 0..number_of_wasms {
            let wasm = dna_wasm_fixturator.next().unwrap();
            wasms.insert(
                tokio_helper::block_forever_on(
                    async { WasmHash::with_data(&wasm).await },
                ),
                wasm,
            );
        }
        wasms.into()
    };
    curve Predictable {
        let mut wasms = BTreeMap::new();
        let mut dna_wasm_fixturator = DnaWasmFixturator::new_indexed(Predictable, get_fixt_index!());
        for _ in 0..3 {
            let wasm = dna_wasm_fixturator.next().unwrap();
            wasms.insert(
                tokio_helper::block_forever_on(
                    async { WasmHash::with_data(&wasm).await },
                ),
                wasm,
            );
        }
        wasms.into()
    };
);

fixturator!(
    DnaFile;
    curve Empty {
        DnaFile::from_parts(
            DnaDefFixturator::new(Empty).next().unwrap().into_hashed(),
            WasmMapFixturator::new(Empty).next().unwrap(),
        )
    };
    curve Unpredictable {
        // align the wasm hashes across the file and def
        let mut zome_name_fixturator = ZomeNameFixturator::new(Unpredictable);
        let wasms = WasmMapFixturator::new(Unpredictable).next().unwrap();
        let mut zomes: IntegrityZomes = Vec::new();
        for (hash, _) in wasms {
            zomes.push((
                zome_name_fixturator.next().unwrap(),
                IntegrityZomeDef::from_hash(
                    hash.to_owned()
                ),
            ));
        }
        let mut dna_def = DnaDefFixturator::new(Unpredictable).next().unwrap();
        dna_def.integrity_zomes = zomes;
        let dna = dna_def.into_hashed();
        DnaFile::from_parts(dna, WasmMapFixturator::new(Unpredictable).next().unwrap())
    };
    curve Predictable {
        // align the wasm hashes across the file and def
        let mut zome_name_fixturator =
            ZomeNameFixturator::new_indexed(Predictable, get_fixt_index!());
        let wasms = WasmMapFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap();
        let mut zomes: IntegrityZomes = Vec::new();
        for (hash, _) in wasms {
            zomes.push((
                zome_name_fixturator.next().unwrap(),
                IntegrityZomeDef::from_hash(
                    hash.to_owned()
                ),
            ));
        }
        let mut dna_def = DnaDefFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap();
        dna_def.integrity_zomes = zomes;
        let dna = dna_def.into_hashed();
        DnaFile::from_parts(
            dna,
            WasmMapFixturator::new_indexed(Predictable, get_fixt_index!())
                .next()
                .unwrap(),
        )
    };
);

// fixturator!(
//     LinkMetaVal;
//     constructor fn new(ActionHash, EntryHash, Timestamp, u8, LinkTag);
// );

// impl Iterator for LinkMetaValFixturator<(EntryHash, LinkTag)> {
//     type Item = LinkMetaVal;
//     fn next(&mut self) -> Option<Self::Item> {
//         let mut f = fixt!(LinkMetaVal);
//         f.target = self.0.curve.0.clone();
//         f.tag = self.0.curve.1.clone();
//         Some(f)
//     }
// }

fixturator!(
    MetaLairClient;
    curve Empty {
        tokio_helper::block_forever_on(async {
            // an empty keystore
            holochain_keystore::test_keystore::spawn_test_keystore().await.unwrap()
        })
    };
    curve Unpredictable {
        // TODO: Make this unpredictable
        tokio_helper::block_forever_on(async {
            holochain_keystore::test_keystore::spawn_test_keystore().await.unwrap()
        })
    };
    // a prepopulate keystore with hardcoded agents in it
    curve Predictable test_keystore();
);

fixturator!(
    SignalBroadcaster;
    curve Empty {
        SignalBroadcaster::new(Vec::new())
    };
    curve Unpredictable {
        SignalBroadcaster::new(Vec::new())
    };
    curve Predictable {
        SignalBroadcaster::new(Vec::new())
    };
);

// XXX: This may not be great to just grab an environment for this purpose.
//      It is assumed that this value is never really used in any "real"
//      way, because previously, it was implemented as a null pointer
//      wrapped in an UnsafeZomeCallWorkspace
fixturator!(
    HostFnWorkspace;
    curve Empty {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db();
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            fake_genesis(authored_db.to_db(), dht_db.to_db(), keystore.clone()).await.unwrap();
            HostFnWorkspace::new(
                authored_db.to_db(),
                dht_db.to_db(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(fixt!(AgentPubKey, Predictable, get_fixt_index!())),
                Arc::new(fixt!(DnaDef))
            ).await.unwrap()
        })
    };
    curve Unpredictable {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db();
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            fake_genesis(authored_db.to_db(), dht_db.to_db(), keystore.clone()).await.unwrap();
            HostFnWorkspace::new(
                authored_db.to_db(),
                dht_db.to_db(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(fixt!(AgentPubKey, Predictable, get_fixt_index!())),
                Arc::new(fixt!(DnaDef))
            ).await.unwrap()
        })
    };
    curve Predictable {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db_with_id(get_fixt_index!() as u8);
        let agent = fixt!(AgentPubKey, Predictable, get_fixt_index!());
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            crate::test_utils::fake_genesis_for_agent(authored_db.to_db(), dht_db.to_db(), agent.clone(), keystore.clone()).await.unwrap();
            HostFnWorkspace::new(
                authored_db.to_db(),
                dht_db.to_db(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(agent),
                Arc::new(fixt!(DnaDef))
            ).await.unwrap()
        })
    };
);

fixturator!(
    HostFnWorkspaceRead;
    curve Empty {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db();
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            fake_genesis(authored_db.to_db(), dht_db.to_db(), keystore.clone()).await.unwrap();
            HostFnWorkspaceRead::new(
                authored_db.to_db().into(),
                dht_db.to_db().into(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(fixt!(AgentPubKey, Predictable, get_fixt_index!())),
                Arc::new(fixt!(DnaDef))
            ).await.unwrap()
        })
    };
    curve Unpredictable {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db();
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            fake_genesis(authored_db.to_db(), dht_db.to_db(), keystore.clone()).await.unwrap();
            HostFnWorkspaceRead::new(
                authored_db.to_db().into(),
                dht_db.to_db().into(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(fixt!(AgentPubKey, Predictable, get_fixt_index!())),
                Arc::new(fixt!(DnaDef)),
            ).await.unwrap()
        })
    };
    curve Predictable {
        let authored_db = holochain_state::test_utils::test_authored_db_with_id(get_fixt_index!() as u8);
        let dht_db = holochain_state::test_utils::test_dht_db_with_id(get_fixt_index!() as u8);
        let cache = holochain_state::test_utils::test_cache_db_with_id(get_fixt_index!() as u8);
        let agent = fixt!(AgentPubKey, Predictable, get_fixt_index!());
        let keystore = holochain_state::test_utils::test_keystore();
        tokio_helper::block_forever_on(async {
            crate::test_utils::fake_genesis_for_agent(authored_db.to_db(), dht_db.to_db(), agent.clone(), keystore.clone()).await.unwrap();
            HostFnWorkspaceRead::new(
                authored_db.to_db().into(),
                dht_db.to_db().into(),
                DhtDbQueryCache::new(dht_db.to_db().into()),
                cache.to_db(),
                keystore,
                Some(agent),
                Arc::new(fixt!(DnaDef))
            ).await.unwrap()
        })
    };
);

fn make_call_zome_handle() -> CellConductorReadHandle {
    Arc::new(MockCellConductorReadHandleT::new())
}

fixturator!(
    CellConductorReadHandle;
    vanilla fn make_call_zome_handle();
);

fixturator!(
    ZomeCallHostAccess;
    constructor fn new(HostFnWorkspace, MetaLairClient, HolochainP2pDna, SignalBroadcaster, CellConductorReadHandle);
);

fixturator!(
    EntryDefsInvocation;
    constructor fn new();
);

fixturator!(
    EntryDefsHostAccess;
    constructor fn new();
);

fixturator!(
    InitInvocation;
    constructor fn new(DnaDef);
);

fixturator!(
    InitHostAccess;
    constructor fn new(HostFnWorkspace, MetaLairClient, HolochainP2pDna, SignalBroadcaster, CellConductorReadHandle);
);

fixturator!(
    MigrateAgentInvocation;
    constructor fn new(DnaDef, MigrateAgent);
);

fixturator!(
    MigrateAgentHostAccess;
    constructor fn new(HostFnWorkspace);
);

fixturator!(
    PostCommitInvocation;
    constructor fn new(CoordinatorZome, SignedActionHashedVec);
);

fixturator!(
    PostCommitHostAccess;
    constructor fn new(HostFnWorkspace, MetaLairClient, HolochainP2pDna, SignalBroadcaster);
);

fixturator!(
    ZomesToInvoke;
    constructor fn one(Zome);
);

fixturator!(
    ValidateHostAccess;
    constructor fn new(HostFnWorkspace, HolochainP2pDna);
);

fixturator!(
    HostContext;
    variants [
        ZomeCall(ZomeCallHostAccess)
        Validate(ValidateHostAccess)
        Init(InitHostAccess)
        EntryDefs(EntryDefsHostAccess)
        MigrateAgent(MigrateAgentHostAccess)
        PostCommit(PostCommitHostAccess)
    ];
);

fixturator!(
    InvocationAuth;
    constructor fn new(AgentPubKey, CapSecret);
);

fixturator!(
    CallContext;
    constructor fn new(Zome, FunctionName, HostContext, InvocationAuth);
);

fixturator!(
    ZomeCallInvocation;
    curve Empty ZomeCallInvocation {
        cell_id: CellIdFixturator::new(Empty).next().unwrap(),
        zome: ZomeFixturator::new(Empty).next().unwrap(),
        cap_secret: Some(CapSecretFixturator::new(Empty).next().unwrap()),
        fn_name: FunctionNameFixturator::new(Empty).next().unwrap(),
        payload: ExternIoFixturator::new(Empty).next().unwrap(),
        provenance: AgentPubKeyFixturator::new(Empty).next().unwrap(),
        signature: SignatureFixturator::new(Empty).next().unwrap(),
        nonce: Nonce256Bits::try_from(ThirtyTwoBytesFixturator::new(Empty).next().unwrap()).unwrap(),
        expires_at: TimestampFixturator::new(Empty).next().unwrap(),
    };
    curve Unpredictable ZomeCallInvocation {
        cell_id: CellIdFixturator::new(Unpredictable).next().unwrap(),
        zome: ZomeFixturator::new(Unpredictable).next().unwrap(),
        cap_secret: Some(CapSecretFixturator::new(Unpredictable).next().unwrap()),
        fn_name: FunctionNameFixturator::new(Unpredictable).next().unwrap(),
        payload: ExternIoFixturator::new(Unpredictable).next().unwrap(),
        provenance: AgentPubKeyFixturator::new(Unpredictable).next().unwrap(),
        signature: SignatureFixturator::new(Unpredictable).next().unwrap(),
        nonce: Nonce256Bits::try_from(ThirtyTwoBytesFixturator::new(Unpredictable).next().unwrap()).unwrap(),
        // @todo should this be less predictable?
        expires_at: (Timestamp::now() + std::time::Duration::from_secs(10)).unwrap(),
    };
    curve Predictable ZomeCallInvocation {
        cell_id: CellIdFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap(),
        zome: ZomeFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap(),
        cap_secret: Some(CapSecretFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap()),
        fn_name: FunctionNameFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap(),
        payload: ExternIoFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap(),
        provenance: AgentPubKeyFixturator::new_indexed(Predictable, get_fixt_index!())
            .next()
            .unwrap(),
        signature: SignatureFixturator::new_indexed(Predictable, get_fixt_index!()).next().unwrap(),
        nonce: Nonce256Bits::try_from(ThirtyTwoBytesFixturator::new_indexed(Predictable, get_fixt_index!()).next().unwrap()).unwrap(),
        // @todo should this be more predictable?
        expires_at: (Timestamp::now() + std::time::Duration::from_secs(10)).unwrap(),
    };
);

/// Fixturator curve for a named zome invocation
/// cell id, test wasm for zome to call, function name, host input payload
pub struct NamedInvocation(pub CellId, pub TestWasm, pub String, pub ExternIO);

impl Iterator for ZomeCallInvocationFixturator<NamedInvocation> {
    type Item = ZomeCallInvocation;
    fn next(&mut self) -> Option<Self::Item> {
        let mut ret = ZomeCallInvocationFixturator::new(Unpredictable)
            .next()
            .unwrap();
        ret.cell_id = self.0.curve.0.clone();
        ret.zome = CoordinatorZome::from(self.0.curve.1).erase_type();
        ret.fn_name = self.0.curve.2.clone().into();
        ret.payload = self.0.curve.3.clone();

        // simulate a local transaction by setting the cap to empty and matching the provenance of
        // the call to the cell id
        ret.cap_secret = None;
        ret.provenance = ret.cell_id.agent_pubkey().clone();

        Some(ret)
    }
}