You need to use Blender 3.3 and I believe the curve is "glTF-safe" if the handles are evenly spaced along the X-axis ie. they divide the interval between the keyframes into thirds.
About how glTF tangents work: The in-tangent and out-tangent in glTF are just the left-hand and right-hand derivatives with respect to time of the curve at the keyframe.
The glTF exporter does indeed export these wrong in 3.2, but a recent PR (merged two days ago) may have fixed this. So first of all you need to use 3.3 alpha from https://builder.blender.org. If all is well, both the value and the derivative should match at the keyframe points.
In glTF, these four data points, the value and the derivative at both keyframes, uniquely determine the curve at all times in an interval. But not so in Blender. For example, look at the below picture of two curves that differ only in the position of the selected handle.
As you can see, despite the slope of the highlighted line being the same, the right one is more "humpy" in the middle. This means even if the values and the derivatives in the glTF are correct at the keyframes, it doesn't follow the rest of the curve is correct. Blender's curve depends on six parameters, not four, so in general conversion to glTF will be lossy. (Basically Blender does cubic interpolation in both X and Y directions, while glTF does only the Y direction.)
Which curves can be exactly converted? Based on a calculation I did a few years ago, a glTF curve exactly converts to a Blender curve where the X coordinates of the handles are 1/3 and 2/3 of the way along the interval, so this form (pictured in the tl;dr) should also convert exactly to a glTF curve. Be warned: this is a theoretical calculation, I have never tested it, it may be wrong :).
PS. This is only one of many, many reasons a Blender curve cannot be exactly converted to a glTF curve naively ie. without sampling. This is why "Always Sample Animations" is on by default. If possible, prefer to leave it on; you will avoid these kinds of problems.
PPS. The glTF importer does not support cubic spline curves at all; it just throws the tangents away, never setting the handles. So don't bother trying to use the importer to check if the exporter is correct.