anastasis-core: fix policy suggestion and add test case
This commit is contained in:
parent
6d7fffc242
commit
8da58bd494
@ -1,6 +1,7 @@
|
|||||||
// This file is auto-generated, do not modify.
|
// This file is auto-generated, do not modify.
|
||||||
// Generated from v0.2.0-4-g61ea83c on Tue, 05 Oct 2021 10:40:32 +0200
|
// Generated from v0.2.0-4-g61ea83c on Tue, 05 Oct 2021 10:40:32 +0200
|
||||||
// To re-generate, run contrib/gen-ts.sh from the main anastasis code base.
|
// To re-generate, run contrib/gen-ts.sh from the main anastasis code base.
|
||||||
|
// XXX: Modified for demo, allowing demo providers for EUR
|
||||||
|
|
||||||
export const anastasisData = {
|
export const anastasisData = {
|
||||||
providersList: {
|
providersList: {
|
||||||
@ -15,6 +16,14 @@ export const anastasisData = {
|
|||||||
url: "https://kudos.demo.anastasis.lu/",
|
url: "https://kudos.demo.anastasis.lu/",
|
||||||
currency: "KUDOS",
|
currency: "KUDOS",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
url: "https://anastasis.demo.taler.net/",
|
||||||
|
currency: "EUR",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
url: "https://kudos.demo.anastasis.lu/",
|
||||||
|
currency: "EUR",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
url: "http://localhost:8086/",
|
url: "http://localhost:8086/",
|
||||||
currency: "TESTKUDOS",
|
currency: "TESTKUDOS",
|
||||||
|
44
packages/anastasis-core/src/policy-suggestion.test.ts
Normal file
44
packages/anastasis-core/src/policy-suggestion.test.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { j2s } from "@gnu-taler/taler-util";
|
||||||
|
import test from "ava";
|
||||||
|
import { ProviderInfo, suggestPolicies } from "./policy-suggestion.js";
|
||||||
|
|
||||||
|
test("policy suggestion", async (t) => {
|
||||||
|
const methods = [
|
||||||
|
{
|
||||||
|
challenge: "XXX",
|
||||||
|
instructions: "SMS to 123",
|
||||||
|
type: "sms",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
challenge: "XXX",
|
||||||
|
instructions: "What is the meaning of life?",
|
||||||
|
type: "question",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
challenge: "XXX",
|
||||||
|
instructions: "email to foo@bar.com",
|
||||||
|
type: "email",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const providers: ProviderInfo[] = [
|
||||||
|
{
|
||||||
|
methodCost: {
|
||||||
|
sms: "KUDOS:1",
|
||||||
|
},
|
||||||
|
url: "prov1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
methodCost: {
|
||||||
|
question: "KUDOS:1",
|
||||||
|
},
|
||||||
|
url: "prov2",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const res1 = suggestPolicies(methods, providers);
|
||||||
|
t.assert(res1.policies.length === 1);
|
||||||
|
const res2 = suggestPolicies([...methods].reverse(), providers);
|
||||||
|
t.assert(res2.policies.length === 1);
|
||||||
|
|
||||||
|
const res3 = suggestPolicies(methods, [...providers].reverse());
|
||||||
|
t.assert(res3.policies.length === 1);
|
||||||
|
});
|
@ -84,9 +84,16 @@ function assignProviders(
|
|||||||
for (const provSel of providerSelections) {
|
for (const provSel of providerSelections) {
|
||||||
// First, check if selection is even possible with the methods offered
|
// First, check if selection is even possible with the methods offered
|
||||||
let possible = true;
|
let possible = true;
|
||||||
for (const methIndex in provSel) {
|
for (const methSelIndex in provSel) {
|
||||||
const provIndex = provSel[methIndex];
|
const provIndex = provSel[methSelIndex];
|
||||||
|
if (typeof provIndex !== "number") {
|
||||||
|
throw Error("invariant failed");
|
||||||
|
}
|
||||||
|
const methIndex = methodSelection[methSelIndex];
|
||||||
const meth = methods[methIndex];
|
const meth = methods[methIndex];
|
||||||
|
if (!meth) {
|
||||||
|
throw Error("invariant failed");
|
||||||
|
}
|
||||||
const prov = providers[provIndex];
|
const prov = providers[provIndex];
|
||||||
if (!prov.methodCost[meth.type]) {
|
if (!prov.methodCost[meth.type]) {
|
||||||
possible = false;
|
possible = false;
|
||||||
@ -96,7 +103,6 @@ function assignProviders(
|
|||||||
if (!possible) {
|
if (!possible) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate diversity, always prefer policies
|
// Evaluate diversity, always prefer policies
|
||||||
// that increase diversity.
|
// that increase diversity.
|
||||||
const providerSet = new Set<string>();
|
const providerSet = new Set<string>();
|
||||||
@ -163,9 +169,18 @@ function assignProviders(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A provider selection maps a method selection index to a provider index.
|
* A provider selection maps a method selection index to a provider index.
|
||||||
|
*
|
||||||
|
* I.e. "PSEL[i] = x" means that provider with index "x" should be used
|
||||||
|
* for method with index "MSEL[i]"
|
||||||
*/
|
*/
|
||||||
type ProviderSelection = number[];
|
type ProviderSelection = number[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method selection "MSEL[j] = y" means that policy method j
|
||||||
|
* should use method y.
|
||||||
|
*/
|
||||||
|
type MethodSelection = number[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute provider mappings.
|
* Compute provider mappings.
|
||||||
* Enumerates all n-combinations with repetition of m providers.
|
* Enumerates all n-combinations with repetition of m providers.
|
||||||
@ -184,7 +199,7 @@ function enumerateProviderMappings(
|
|||||||
}
|
}
|
||||||
for (let j = start; j < m; j++) {
|
for (let j = start; j < m; j++) {
|
||||||
a[i] = j;
|
a[i] = j;
|
||||||
sel(i + 1, j);
|
sel(i + 1, 0);
|
||||||
if (limit && selections.length >= limit) {
|
if (limit && selections.length >= limit) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -199,8 +214,6 @@ interface PolicySelectionResult {
|
|||||||
policy_providers: PolicyProvider[];
|
policy_providers: PolicyProvider[];
|
||||||
}
|
}
|
||||||
|
|
||||||
type MethodSelection = number[];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute method selections.
|
* Compute method selections.
|
||||||
* Enumerates all n-combinations without repetition of m methods.
|
* Enumerates all n-combinations without repetition of m methods.
|
||||||
|
Loading…
Reference in New Issue
Block a user