Rewrite rule and display of post

I don’t understand why but the regex seemed to be the last issue. It was catching it but even with the post name hardcoded in it didn’t work correctly. This is the final working version.

add_filter( 'post_link', 'custom_permalink', 10, 3 ); 
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules'); 

function custom_permalink( $permalink, $post, $leavename ) {
  $category = get_the_category($post->ID); 
  if (  !empty($category) && $category[0]->cat_name == "Test" )
  {
      $permalink = trailingslashit( home_url('test/' . $post->post_name ) );
  }
  return $permalink;
}

function flushRules(){
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}

function wp_insertMyRewriteRules($rules)
{
  $newrules = array();
  $newrules['^test/(.*)$'] = 'index.php?name=$matches[1]';
  return $newrules + $rules;
}