In this problem, why is the work done by the spring not equal to the line integral of the spring force over its displacement? Problem statement:

In the given figure, two blocks A and B, each having a mass of 320 grams are connected by a light string passing over a smooth light pulley. The horizontal surface on which block A can slide is smooth. The block A is attached to a spring of spring constant 40$\frac{N}m$ whose other end is fixed to a support 40cm above the horizontal surface. Initially, the spring is vertical and unstretched when the system is released to move. Find the velocity of the block A at the instant it breaks off the surface below it. Take g=10$\frac{m}{s^2}$.
What I have got till now:
After drawing the free body diagram of the block at the instant it breaks off the surface, I found these pieces of data:

*

*The block breaks off when the smaller angle of spring with the ground is $\sin^{-1}\frac{4}5$


*The distance that the block covers till this point is 0.3m


*The elongation in the spring is equal to 0.1m
The part that I am confused with:
Now, according to what I have learned, the work done by any force on an object is $\vec{F}•d\vec{r}$. So this means that we have to take the varying angle between the spring and the displacement of the block into account while calculating the work done by it. So, the work done by spring should be:
$\int\ k\Delta s\cos\theta dx$Here, $\Delta s$ is the elongation of the spring, $\theta$ is the smaller angle made by the spring with the ground, and we are integrating in terms of the displacement of the block.After a bit of observation, I found that $dx$ is equal to -0.4$d\theta$. and $\Delta s$ is equal to 0.4$\frac{1-sin\theta}{sin\theta}$. Now, we can integrate in terms of $\theta$ and find the work done by the spring, which comes to about 2.32 Then, we can factor in the potential energy and find the kinetic energy from that.But this is not the solution that I found when I searched through the internet. A couple of solutions I found online:
https://www.toppr.com/ask/question/figure-shows-two-blocks-a-and-b-each-having-a-mass-of-320-gram-connected/
https://www.sarthaks.com/44038/figure-8-e12-shows-two-blocks-a-and-b-each-having-a-mass-of-320-g-connected-by-a-lightAt the end of both the solutions, the work done by spring is given as $\frac{1}2kx^2$. Which should not be correct as the force is not acting parallel or anti parallel to displacement. The final answer is correct:$1.5\frac{m}{s}$, and the one I got is wrong. But, it does not make sense to me to put the work done by spring as $\frac{1}2kx^2$.So, am I making a very stupid mistake here, or is there something else going on?Question from: HC Verma- Concepts of Physics Part 1 Chapter 8 Q.50
 A: In the formula $W= \frac {kx^2}{2}$, the $x$ is the extension in the spring, not the displacement of the block. As the spring is pivoted and free to rotate, even if the displacement of the block is making some angle with the force, the extension in the spring is always parallel to the force. Thus, the assumptions made while deriving the formula still hold true.
The answer can also be obtained by the method you tried, however you made a mistake in the derivation. From the figure, I got the equation
$$x= 0.4 \cot {(\theta)}$$
$$\therefore dx = -0.4 \csc ^2 {(\theta )}d\theta$$
While you seem to have got
$$dx = -0.4 \: d \theta$$
A: In this example, we have to assume that the spring is is attached to the block in such a way that it allows free rotation of lower end of the spring as if it is hinged to the block. In this case the spring will apply force longitudinally so that the elongation produced in the spring will be equal to the difference between its final and initial lengths. This will give us the correct elongation because if the lower end of the spring is free to rotate then the elongation produced and the spring force will always remain inline. So no need to use integration.
A: With Hooke's law, we model the spring as supplying a conservative force. Because of this, we can calculate the work done on a spring by looking just at its final state, regardless of the history that resulted in that state. It's similar to finding the work done on a car on a roller coaster track. You can express its velocity in terms of $t$, take the dot product with the force of gravity, and integrate, or you can just take the net change in height times its weight. You might find it a useful exercise to work out the spring problem the "hard way", though, to understand how these calculations are done. Your explanation of how you did this has several problems.

After a bit of observation, I found that dx is equal to -0.4dθ.

It would help if you showed how you calculated that. If $x$ is the horizontal distance that the block has traveled from the start, I have that if $s$ is the original spring length, then $s+\Delta s$ makes up the hypotenuse of a right triangle with legs $s$ and $x$, and angle $\theta$. Since $\tan (\theta)$ is the ratio between the legs, we have $\tan (\theta) = \frac s x$, or $x = s\cot(\theta)$. This gives $dx = -s\csc^2(\theta)d\theta$. You have a $.4$ in your expression, which matches $s$, but you don't have $\csc^2(\theta)$. You seem to have not noticed that $\frac{d\theta}{dx}$ is not constant with respect to $x$.
So converting to $\theta$ gets rather nasty. Suppose we instead integrate with respect to $l$, where $l$ is the total length of the spring. Then $s^2+x^2=l^2$ and implicit differentiation gives us $2sds+2xdx=2ldl$. $s$ is a constant, so $ds = 0$. This gives $dx = \frac l x dl$. We have that $\cos (\theta) = \frac x l$, so $\cos(\theta)dx = dl$. Now $\int k(\Delta s)\cos(\theta) dx$ becomes just $\int k (\Delta s) dl$. Since $l = s+\Delta s$, $\Delta s = l-s$, giving $\int k (l-s)dl$. You could also integrate with respect to $\Delta s$, but I didn't want to write $\int k (\Delta s)d(\Delta s)$.
Also a few notes on MathJax: the formatting system "knows" some math terms, such as "cos", and putting a backslash before them will put them in non-italic font. That, as well as enclosing things in parentheses, makes expressions easier to read. For instance, \int k(\Delta s)\cos(\theta) dx rather than \int k\Delta s cos\theta dx.
