Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence are not as transparent as they should #1665

Open
Jym77 opened this issue Aug 5, 2024 · 0 comments
Open

Sequence are not as transparent as they should #1665

Jym77 opened this issue Aug 5, 2024 · 0 comments
Labels
bug Report of unexpected or faulty behaviour in Alfa

Comments

@Jym77
Copy link
Contributor

Jym77 commented Aug 5, 2024

While working on alfa-test-utils, I've noticed a weird case where forcing a sequence or not changes the results of a function, which should not happen due to referencial transparency.
This seems to be linked to use of .groupBy as before using it nothing happened. Maybe also linked to hashability of string Enum.

  export async function run(
    page: Page,
    options: Options = {}
  ): Promise<Result> {
    const rulesToRun =
      options.rules?.override ?? false
        ? options.rules?.custom ?? []
        : filter(Rules.allRules, options.rules).concat(
            options.rules?.custom ?? []
          );

    const outcomes = Sequence.from(
      await alfaAudit.of(page, rulesToRun).evaluate()
    );
    const ResultAggregates = aggregates(outcomes);

    return {
      alfaVersion,
      page,
      outcomes: filter(outcomes, options.outcomes),
      ResultAggregates,
    };
  }

export function aggregates(
    outcomes: Sequence<alfaOutcome>
  ): Iterable<RuleAggregate> {
    return (
      outcomes
        // Group by rule URI
        .groupBy((outcome) => outcome.rule.uri)
        // For each rule, group by outcome
        .map((ruleOutcomes) =>
          ruleOutcomes.groupBy((outcome) => outcome.outcome)
        )
        // Count the size of each group and build the aggregates
        .map((groups, uri) => ({
          RuleId: uri,
          Failed: groups.get(Outcome.Value.Failed).getOrElse(Sequence.empty)
            .size,
          Passed: groups.get(Outcome.Value.Passed).getOrElse(Sequence.empty)
            .size,
          CantTell: groups.get(Outcome.Value.CantTell).getOrElse(Sequence.empty)
            .size,
        }))
        .values()
    );
  }

Inlining the computation of ResultAggregates discards all but the first outcomes from outcomes. Forcing the evaluation of the sequence (e.g. with a dummy [...outcomes], or … .toJSON() (which makes investigations tricky)) restores every thing. None of these should be impacting the result.

Given that this is the first time we notice this, I'm letting it as is for now.

@Jym77 Jym77 added the bug Report of unexpected or faulty behaviour in Alfa label Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Report of unexpected or faulty behaviour in Alfa
Projects
Status: 📮 Backlog
Development

No branches or pull requests

1 participant