How to allow WordPress to recognize a custom URL segment after a product URL and use a custom template?

Looks like all I needed to do was put the template_include filter inside of the init action. Source: https://stackoverflow.com/a/50037707/5749974

add_action( 'init', function () {
    $product_base = ltrim( get_option( 'woocommerce_permalinks' )[ 'product_base' ], "https://wordpress.stackexchange.com/" );
    add_rewrite_rule( "{$product_base}/([^/]+)/test", 'index.php?product=$matches[1]&test=1', 'top' );

    add_filter( 'template_include', function( $template ) {
        if ( get_query_var( 'test' ) ) {
            $template = get_stylesheet_directory() . '/templates/test.php';
        }
     
        return $template;
    } );
} );

Now https://example.com/products/my-product and https://example.com/products/my-product/test both load correctly!