Why Testing Matters0:08
I wanna talk to you about the importance of testing. Let's start with an example. I'm gonna use the Claude agent and I'll paste in a prompt for brevity. Okay? So this relates to our support ticket dashboard. Uh, effectively when you view a support ticket, we show labels that have been assigned, but there's currently no way to remove a label. And we want the ai, uh, to fix this. And real quick, let me just show you an example.
And we want the ai, uh, to fix this. And real quick, let me just show you an example. And this is very much a work in progress, mind you. But sure enough, we can see a list of labels at the top, but there's no way to remove that label from the ticket if I need to. So that's what we're gonna work on now. Alright, that's done. But take a look at this. So, perfect. I've successfully implemented the label removal feature. It updated the pill component, which is the label.
Spotting Missing Tests0:48
I've successfully implemented the label removal feature. It updated the pill component, which is the label. It created a controller, which responds to the request, it added the route, it updated the page. This is fine, but here's the problem. It didn't include any tests as part of this, and that's a really big problem. Here's why. If I were to take a look at the controller itself, alright, so support ticket label controller.
to take a look at the controller itself, alright, so support ticket label controller. And sure enough, we have a destroy action because we're, we're kind of destroying the label. We're following a cruddy by design, uh, approach. Oh, and by the way, if you're working along and AI doesn't do that for you, update your guidelines, tell it to, and it will adhere to that. Otherwise, it might have something like remove label and it'll do stuff like that.
Otherwise, it might have something like remove label and it'll do stuff like that. It's fine if that's what you want, but I like to stick to the seven resourceful actions. Okay, anyways, so here, um, on a support ticket, label labels is like an array and we use the as array object. Um, cast take a look. Support ticket. Yeah, just very quickly, let me scroll down to cast. Yeah. So labels is just, um, as array object,
Yeah, just very quickly, let me scroll down to cast. Yeah. So labels is just, um, as array object, which gives us an easy way to interact with it and remove and unset items from it. Okay? Anyways, though, notice right here it's using this funky thing where it calls get array copy, and then it uses a collection to reject the item being passed through. This is just not how I would do it.
to reject the item being passed through. This is just not how I would do it. So here's the problem though, is even if I were to ask AI to make the fix, it might try, but it has no verification to prove that it was successful. And this is where tests really come into play. We wanna be able to say, look, here are some tests that currently return green. I want you to change such and such and you will know you are successful because
I want you to change such and such and you will know you are successful because after your change, the tests still continue to return green, right? Right Now we don't have that at all. So we can't successfully refactor. This is a, a human, uh, lesson we learn and it also applies to AI that much more actually. So here's what we're gonna do. I'm gonna go into my guidelines file, and that will be within AI guidelines.
Updating AI Guidelines2:47
So here's what we're gonna do. I'm gonna go into my guidelines file, and that will be within AI guidelines. And let's just go into Laravel and I've actually trended this down for the demo. But here's some ideas for things you might, uh, add here. Let's do a new one. When creating a new endpoint and controller slash action, you must create a relevant feature and smoke test to confirm it works. Okay? So now remember whenever you update a guidelines file,
and smoke test to confirm it works. Okay? So now remember whenever you update a guidelines file, and if you are using Laravel Boost, then you need to refresh, uh, your guidelines like this PB Artisan Boost install. So that will regenerate the, um, the, uh, guidelines file, whether it's agents MD or claw.md or Junie. And it will make sure that these take precedence at the top of the file. Cool. Next, let's come back to our agent.
Generating Feature Tests3:38
precedence at the top of the file. Cool. Next, let's come back to our agent. And I'm actually gonna start a new chat, even though we're losing a little bit of context. That's okay. Four, support ticket label controller. We don't have any feature tests. Review your guidelines, file and implement one to confirm that label removal works as expected. Yeah. And this new reality where AI is generating so much
works as expected. Yeah. And this new reality where AI is generating so much of the code, the debate about whether to write tests or not is gone. You have to do it. It's a requirement. All right, that's done. So let's take a look. Alright, let's see. Uh, so we have some authentication or authorization stuff. Guests cannot remove labels. Fine. What else? Uh, removing a label from a ticket
Guests cannot remove labels. Fine. What else? Uh, removing a label from a ticket that has no labels, does nothing. Yeah. So if you make that request to remove a label and the ticket never had it in the first place, then we need to handle that. Um, this is fine. Maybe a touch more verbose than I would reach for. Um, but that's fine. Um, it works. So I can run all of these tests.
Refactoring with Test Safety4:43
Um, but that's fine. Um, it works. So I can run all of these tests. And now here's the cool thing. If I wanna continue working by hand, old school style, that's fine because I have the test backing me up. But further, if I want AI to make the change, uh, it has the same benefit, right? So let's go into that controller now. Support ticket label controller. Yeah. So you'll remember this was the
Support ticket label controller. Yeah. So you'll remember this was the section that confused me, right? Get array object. This just seems more verbose than it needs to be. For example, let's see, could we do this? Uh, if empty labels, let's give that a run. All right, still passes. Next, uh, let's just first read it. Could I get rid of that? And then I could say labels equal ticket labels.
Could I get rid of that? And then I could say labels equal ticket labels. So we're just gonna run this one step at a time. Yeah, that still works. Next, can we inline it? All right, get a run. Yeah, small refactors. This is the way we did things back in bad day. Now by the way, if you're thinking, why can't we just do something like unset labels label? Um, that's because of the structure that I have set up for the labels.
Um, that's because of the structure that I have set up for the labels. Uh, but instead we could treat it like a collection here. And rather than reject, we might even filter it down, filter down the label where the label does not equal the lowercase form of the past in label. Uh, we're gonna re-key them. So that's why we call values. And then we convert that back to an array. You could even do two array if that's more clear to you. Anyways, we're just making a little changes,
You could even do two array if that's more clear to you. Anyways, we're just making a little changes, but the entire time I can refactor with the assurance. I could even tweak this further. Ticket labels equal that, and then just simply call ticket save at the end. If you like that more. Did that break anything? No. Uh, that test is still passing. And in fact, if I switch back, let's run all the tests and those are passing as well.
Tests Are Non-Negotiable6:31
And in fact, if I switch back, let's run all the tests and those are passing as well. You get the idea. So here's the point. If you're working with ai, it's gonna generate a lot of code, um, that you're not necessarily going over with a fine tooth comb. You will supervise it, you will make sure it's following the right structure, but you're not reading every single line the way you might've done a couple years ago.
but you're not reading every single line the way you might've done a couple years ago. So for that reason alone, tests are essential. They cannot be skipped. And if AI does skip them, go back and tell them, uh, to fill in the blanks. It's needed.
