def flatten_test_results(trie, prefix=None):
  """Flattens a trie structure of test results into a single-level map.

  This function flattens a trie to a single-level map, stopping when it reaches
  a nonempty node that has either 'actual' or 'expected' as child keys. For
  example:

  .. code-block:: python

    {
      'foo': {
        'bar': {
          'expected': 'something good',
          'actual': 'something bad'
        },
        'baz': {
          'expected': 'something else good',
          'actual': 'something else bad',
          'quxx': 'some other test metadata'
        }
      }
    }

  would flatten to:

  .. code-block:: python

    {
      'foo/bar': {
        'expected': 'something good',
        'actual': 'something bad'
      },
      'foo/baz': {
        'expected': 'something else good',
        'actual': 'something else bad',
        'quxx': 'some other test metadata'
      }
    }
  """
  # Cloned from webkitpy.layout_tests.layout_package.json_results_generator
  # so that this code can stand alone.
  result = {}
  for name, data in trie.iteritems():
    if prefix:
      name = prefix + '/' + name

    if len(data) and not 'actual' in data and not 'expected' in data:
      result.update(flatten_test_results(data, name))
    else:
      result[name] = data

  return result