Skip to content

Instantly share code, notes, and snippets.

@JhonatanHern
Created January 6, 2021 03:28
Show Gist options
  • Save JhonatanHern/8f112d31d9a33768b4ef8c8658bbefef to your computer and use it in GitHub Desktop.
Save JhonatanHern/8f112d31d9a33768b4ef8c8658bbefef to your computer and use it in GitHub Desktop.
import { Orchestrator, Config, InstallAgentsHapps } from '@holochain/tryorama'
// Set up a Conductor configuration using the handy `Conductor.config` helper.
// Read the docs for more on configuration.
const conductorConfig = Config.gen()
// Construct proper paths for your DNAs
const appDna = '../travol.dna.gz' // path.join(__dirname, 'todo_rename_dna.dna.gz')
// create an InstallAgentsHapps array with your DNAs to tell tryorama what
// to install into the conductor.
const installation: InstallAgentsHapps = [
[ // agent
[ // happ
appDna // dna
]
],
[ // agent
[ // happ
appDna // dna
]
],
[ // agent
[ // happ
appDna // dna
]
]
]
const sleep = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms));
const dateToTimestamp = (date) => [
Math.floor(date / 1000),
(date % 1000) * 1000,
];
const removeHashesFromFares = arr => {
return arr.map(([fare]) => ({
code: fare.code,
marketingName: fare.marketingName,
availableSeats: fare.availableSeats
}))
}
const compareFares = ( a, b ) => {
if ( a.marketingName < b.marketingName ){
return -1;
}
if ( a.marketingName > b.marketingName ){
return 1;
}
return 0;
}
const orchestrator = new Orchestrator()
orchestrator.registerScenario("inventory test", async (s, t) => {
const [players] = await s.players([conductorConfig])
const [[alice_happ], [bob_happ]] = await players.installAgentsHapps(installation)
let flightHash = await alice_happ.cells[0].call(
"inventory",
"create_flight_segment",
{
flight_segment_info: {
origin:"Caracas",
destination:"Madrid",
departure_time: dateToTimestamp(new Date())
},
list_of_fares: [
{
code: "ECON",
marketingName: "Economy",
availableSeats: 80
},
{
code: "FIRST",
marketingName: "First Class",
availableSeats: 20
}
]
}
)
t.ok(flightHash)
await sleep(10)
let curTime = (new Date()).getTime()
let halfAnHour = 60 * 30 * 1000
let emptyFlights = await bob_happ.cells[0].call(
"inventory",
"search_for_flights",
{
origin: "Rio de Janeiro",
destination: "London",
time_range: {
start_time: dateToTimestamp(new Date()),
end_time: dateToTimestamp(new Date(curTime + 4 * halfAnHour))
},
pax:0
}
)
t.equal(emptyFlights.length, 0)
let emptyFlights2 = await bob_happ.cells[0].call(
"inventory",
"search_for_flights",
{
origin: "Caracas",
destination: "Madrid",
time_range: {
start_time: dateToTimestamp(new Date(curTime - 4 * halfAnHour)),
end_time: dateToTimestamp(new Date(curTime - 3 * halfAnHour))
},
pax:0
}
)
t.equal(emptyFlights2.length, 0)
let fullFlights = await bob_happ.cells[0].call(
"inventory",
"search_for_flights",
{
origin: "Caracas",
destination: "Madrid",
time_range: {
start_time: dateToTimestamp(new Date(curTime - 4 * halfAnHour)),
end_time: dateToTimestamp(new Date(curTime + 4 * halfAnHour))
},
pax:0
}
)
t.equal(fullFlights.length, 1)
let fareList = await bob_happ.cells[0].call("inventory", "get_flight_fares", flightHash)
t.deepEqual(removeHashesFromFares(fareList).sort(compareFares), [
{
code: "FIRST",
marketingName: "First Class",
availableSeats: 20
},
{
code: "ECON",
marketingName: "Economy",
availableSeats: 80
}
].sort(compareFares))
})
orchestrator.registerScenario("inventory test", async (s, t) => {
const [players] = await s.players([conductorConfig])
const [[alice_happ], [bob_happ]] = await players.installAgentsHapps(installation)
let aliceKey = await alice_happ.cells[0].call("inventory", "get_my_address", null)
let bobKey = await bob_happ.cells[0].call("inventory", "get_my_address", null)
let hash = await alice_happ.cells[0].call("payment_gateway", "create_order", {
amount: 45.3,
currency_code: "USD",
spender: aliceKey,
recipient: bobKey,
})
t.ok(hash)
await sleep(2000)
let res = await bob_happ.cells[0].call("payment_gateway", "has_payment_been_ordered", hash)
t.ok(res)
})
const getAFareHash = (airline) => {
return new Promise(async (resolve, reject) => {
let flightHash = await airline.cells[0].call(
"inventory",
"create_flight_segment",
{
flight_segment_info: {
origin:"Caracas",
destination:"Madrid",
departure_time: dateToTimestamp(new Date())
},
list_of_fares: [
{
code: "ECON",
marketingName: "Economy",
availableSeats: 80
},
{
code: "FIRST",
marketingName: "First Class",
availableSeats: 20
}
]
}
)
let fareList = await airline.cells[0].call("inventory", "get_flight_fares", flightHash)
resolve(fareList[0][1])
})
}
orchestrator.registerScenario("ticket booking", async (s, t) => {
const [players] = await s.players([conductorConfig])
const [[agency], [airline], [paymentGateway]] = await players.installAgentsHapps(installation)
let agencyKey = await agency.cells[0].call("inventory", "get_my_address", null)
let airlineKey = await airline.cells[0].call("inventory", "get_my_address", null)
let paymentGatewayKey = await paymentGateway.cells[0].call("inventory", "get_my_address", null)
let fareHash = await getAFareHash(airline)
console.log('fareHash: ', fareHash)
let hash = await agency.cells[0].call("ticket_bookings", "book_ticket", {
fare_hash: fareHash,
airline: airlineKey,
payment_gateway: paymentGatewayKey
})
t.ok(hash)
await sleep(1000)
let numberOfTickets = await agency.cells[0].call("ticket_bookings", "get_number_of_tickets_booked", fareHash)
t.equal(numberOfTickets, 1)
let curTime = (new Date()).getTime()
let halfAnHour = 60 * 30 * 1000
let fullFlights = await agency.cells[0].call(
"ticket_bookings",
"search_for_flights",
{
origin: "Caracas",
destination: "Madrid",
time_range: {
start_time: dateToTimestamp(new Date(curTime - 4 * halfAnHour)),
end_time: dateToTimestamp(new Date(curTime + 4 * halfAnHour))
},
pax:0
}
)
t.equal(fullFlights.length, 1)
})
orchestrator.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment